#include <stdio.h>
#include <math.h>
int main()
{
char FirstInitial, LastInitial;
printf("Please enter your first and last initials:\n");
scanf("%c %c", &FirstInitial, LastInitial);
getchar();
printf("%c %c", FirstInitial, LastInitial);
}//END OF MAIN
Trouble Printing Characters
Page 1 of 110 Replies - 752 Views - Last Post: 15 November 2015 - 06:53 AM
#1
Trouble Printing Characters
Posted 12 November 2015 - 08:41 AM
Hey im working on a program for school, and heres what i have its not the whole thing but it wont even print a character.
/>/>
Replies To: Trouble Printing Characters
#2
Re: Trouble Printing Characters
Posted 12 November 2015 - 09:03 AM
Look closely at line 7. What's wrong with LastInitial?
#3
Re: Trouble Printing Characters
Posted 12 November 2015 - 12:44 PM
i'm so dumb thanks so much bro
#4
Re: Trouble Printing Characters
Posted 12 November 2015 - 01:41 PM
stvnalexbro, on 12 November 2015 - 07:44 PM, said:
i'm so dumb thanks so much bro
You might be interested in using GCC as your compiler then, since it will tell you when you make a mess of printf/scanf formats (along with many other dumb trivial errors as well).
$ gcc -Wall foo.c foo.c: In function ‘main’: foo.c:7:3: warning: format ‘%c’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat] foo.c:14:1: warning: control reaches end of non-void function [-Wreturn-type] foo.c:7:8: warning: ‘LastInitial’ is used uninitialized in this function [-Wuninitialized]
#5
Re: Trouble Printing Characters
Posted 12 November 2015 - 01:47 PM
One thing it won't tell you though, is that main needs to return an integer (hence int main()).
#6
Re: Trouble Printing Characters
Posted 12 November 2015 - 02:12 PM
#include <stdio.h>
#include <math.h>
/*int*/ main()
{
char FirstInitial, LastInitial;
printf("Please enter your first and last initials:\n");
scanf("%c %c", &FirstInitial, LastInitial);
getchar();
printf("%c %c", FirstInitial, LastInitial);
}//END OF MAIN
$ gcc -Wall foo.c
foo.c:3:9: warning: return type defaults to ‘int’ [-Wreturn-type]
foo.c: In function ‘main’:
foo.c:7:3: warning: format ‘%c’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]
foo.c:14:1: warning: control reaches end of non-void function [-Wreturn-type]
foo.c:7:8: warning: ‘LastInitial’ is used uninitialized in this function [-Wuninitialized]
> One thing it won't tell you though, is that main needs to return an integer (hence int main()).
Those -Wreturn-type warnings seem to pretty definitive to me.
#8
Re: Trouble Printing Characters
Posted 12 November 2015 - 11:31 PM
Yeah gcc defo does complain about main returning an integer.
#9
Re: Trouble Printing Characters
Posted 13 November 2015 - 12:09 AM
while doing coding pay full attention towards it and read the whole code 2-3 times. intial must be written in small letters but you starts with uppercase. these are such silly mistakes which you can take care of.
#10
Re: Trouble Printing Characters
Posted 13 November 2015 - 05:00 AM
That's odd. All I get is this:
p.cpp:7:34: warning: format specifies type 'char *' but the argument has type
'int' [-Wformat]
scanf("%c %c", &FirstInitial, LastInitial);
~~ ^~~~~~~~~~~
p.cpp:7:34: warning: variable 'LastInitial' is uninitialized when used here
[-Wuninitialized]
scanf("%c %c", &FirstInitial, LastInitial);
^~~~~~~~~~~
p.cpp:5:34: note: initialize the variable 'LastInitial' to silence this warning
char FirstInitial, LastInitial;
^
= '\0'
2 warnings generated.
#11
Re: Trouble Printing Characters
Posted 15 November 2015 - 06:53 AM
alishawalter, on 13 November 2015 - 12:09 AM, said:
while doing coding pay full attention towards it and read the whole code 2-3 times. intial must be written in small letters but you starts with uppercase. these are such silly mistakes which you can take care of.
its doesn't matter as long as its the same as the one you declare
Page 1 of 1

New Topic/Question
Reply


MultiQuote




|