Im a beginner and i am trying to write a program that asks you do you like peaches, so if i type yes i want it to reply that my answer was yes, and if i type no then i want it to reply to me that no you dont like peaches. Here is my code:
int main()
{ int a1,a2,a3,answer;
printf("Do you like peaches? Yes or No?\n");
answer=
scanf("%c%c%c",%26amp;a1,%26amp;a2,%26amp;a3);
if (answer="yes")
printf("yes you do like peaches");
else
printf("no you dont like peaches");
system("pause");
}
any suggestions from people that know it?
C-Programing Question?
Hi as a beginner I used to like learning by example so please try to follow this code
// you need these libraries included for the functions
// to be used in this example
#include %26lt;stdio.h%26gt; // needed printf()
#include %26lt;conio.h%26gt; // needed for getch()
#include %26lt;string.h%26gt; // needed for strcmpi()
void main(void)
{
// set an array of characters long enough to keep our
// answer data that the user types in, this can be shorter
// like up to 4 and still work but the user can type too much
// and overflow it
char answer[30];
// as the question
printf("Do you like peaches? Yes or No?\n");
// we only need to read in 1 answer and it is to be stored in
// the array of characters as a string.
scanf("%s",%26amp;answer);
// flush the input stream in case of bad input - this helps scanf
// not lock up waiting on inputs
fflush(stdin);
// use "strcmpi" to compare the inputted answer
// see below for the definition of this function
if (strcmpi(answer, "yes") == 0)
{
printf("yes you do like peaches\n");
}
else if (strcmpi(answer, "no") == 0)
{
printf("no you dont like peaches\n");
}
else
{
// if anything other than yes or no is inputted throw
// the following message up
printf("I didnt understand your answer\n");
}
// wait for the user to press a key before exiting the program
getch();
}
OK thats it!
Things to note include:
1) the proper use of scanf(). If you expect only 1 input from the user then only scan once. Thats why we only have an array of characters called answer[] to store our string in with the %s.
2) Learn the difference between an assigment and a comparison. Your code had if (answer="yes")
its clear that you want to do a comparison so you would need to have another = sign ie if (answer=="yes")
But this is besides the point as its not valid to compare strings that way.
3) strcmpi() explanation
Syntax
#include %26lt;string.h%26gt;
int strcmpi(const char *s1, const char *s2);
Description - Compares one string to another, without case sensitivity ie your inputted answer may be No Yes nO yEs etc
If s1 is the same as s2 strcmpi returns the value 0. ie that is why we check the return value with the == 0.
4) a "void main(void)" declaration is fine as we dont need to return any value once the progam exits. If we had int main() then we would need a "return 1" or something which is of no use in this example
I cant think of anything else......have a close look at my example and understand whats happening as you go through the sections.
Good luck.
Reply:I'm not going to give you an answer persay but some tidbits to assist in learning...
First why are you using integer variables for a string? You could use something like char answer[3]. Just remember to assign answer[3] = '/0' (or null).
Also in your if statment you are making an assignment not a comparison of equality, plus with strings you should use a string compare function or cycle through each char until you reach null.
Finally you state that main() returns an integer but you return nothing. By default you could just return 0 or make main a void function.
C also has libraries to handle strings with many useful functions, just don't forget to check array boundaries since this is not done for you in C/C++.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment