#Problem with input

25 messages · Page 1 of 1 (latest)

smoky spear
#

#include <stdio.h>
int sum(int a, int b)

{
return a + b;
}
int main()
{
int a, b, c;

printf("Enter your first number");
scanf("%d\n", &a);

printf("Enter your second number");
scanf("%d\n", &b);

c = sum(a, b);
printf("The sum is %d\n", c);

return 0;

}

// When I run this code, int b doesnt execute until i type int a two times . And then it adds both of those int a but not a and b. why is that? pls help

fallow sparrowBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

cloud edge
#

get rid of the \n in the scanf format

#

that's only neccesary for printf, since scanf formatters other than %c and %[ will skip leading whitespace automatically

smoky spear
#

Thank you @cloud edge.

fallow sparrowBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

smoky spear
#

#include <stdio.h>
void printstar(int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("%c", "*");
}
}
int main()
{
int n;
printf("How many stars do you want to print?\n");
scanf("%d", &n);
printstar(n);
return 0;
}

#

What am i doing wrong here?

fallow sparrowBOT
#

smoky spear
#

I want to print "stars" as many times as the user gives input for. I'm calling the function and asking it to print the stars "n" times but it just wont work

cloud edge
#

images aren't working right now, you're gonna have to describe what "doesn't work" means

smoky spear
#

In function 'printstar':
Func.c:27:10: error: 'i' undeclared (first use in this function)
for (i = 0; i < n; i++)
^
Func.c:27:10: note: each undeclared identifier is reported only once for each function it appears in

[Done] exited with code=1 in 0.093 seconds

#

the error message

cloud edge
#

did you press save

smoky spear
cloud edge
#

you're using %c on a char*/string variable

smoky spear
#

can you edit the code and tell me what shoudl i change pls?

cloud edge
#

handing you a solution doesn't help you

#

Your formatters and variable types need to match - %s is for strings, %c is for single characters

smoky spear
#

so do i use %s?

cloud edge
#

What do you think?

smoky spear
#

i think there's something wrong with my loop. That tutorial guy used %c but my %c isnt working

cloud edge
#

"" is for string literals. use '' for a single char

smoky spear
#

oh wow