How do i compare strings. more specificly how do jou do the good old Y/N function. you know "do you want to quit (Y/N) ". ive been trying for ages. thanks
bart
How do i compare strings. more specificly how do jou do the good old Y/N function. you know "do you want to quit (Y/N) ". ive been trying for ages. thanks
bart
Use strcmp to compare strings.
As for your question, I'd put the whole thing in a while loop, kinda like this:
Code:char response = 0; while(response != 'n') { //Do your stuff printf("Do you want to continue?"); scanf("%c",response); }
-Govtcheez
[email protected]
I'm NO expert with strings, but try this:
#include <iostream.h>
int main() {
char a; //Makes a new character
cout << "Menu: \n[1] Yes \n[2] No \n>"; //Makes the menu
cin >> a; //Get the input
/* Some may argue I sould use select here,
but I need to practice it first. */
if (a == 'y') {
// do y stuff
}
if (a == 'n') {
// do n stuff
} else {
// do other stuff
}
return 0;
}
Please correct me if there's a problem with the code .
Hope this helps.
-Save the whales. Collect the whole set.
i use dev c++. i do
#include <iostream.h>// for basic in out functions
#include <stdio.h> // for getchar
char YorN[1]={'n'}; // Yes or No, set standard to no
int main()
{
point: // for errors
cout<<"want to know who created this (Y/N)"; //ask
cin>>YorN; //get the reply
if (YorN == 'y') // if he wants to know
{
cout<<"shuks, im honored. its bart!";
getchar();
return 0; //quit
}
else
{
if (YorN == 'n') // i dint need to put this in,
// anyone wil say yes
{
cout<<"im offended.";
getchar();
return 0; //jes, quit
}
else
cout<<"there was a error. please retype if you ";goto point;
//this is wat happens if i run
}
}
but it keeps on doing cout<<"there was a error. please retype if you ";goto point;
It's cleaned up and far from perfect (bad, bad, goto)
Code:#include <iostream.h> #include <stdio.h> #include <conio.h> char YorN = 'n'; // There's no reason for an array using namespace std; int main() { point: // for errors cout << "want to know who created this (Y/N)"; cin >> YorN; if (YorN == 'y') { cout<<"shucks, im honored. its bart!"; fflush(stdin); getch(); return 0; //quit } else { if (YorN == 'n') { cout<<"im offended."; fflush(stdin); getch(); return 0; } else { cout<<"there was a error. please retype if you "; goto point; } } }
-Govtcheez
[email protected]
Govtcheez you are gonna get whacked by salem's wand of undefined behaviour for using fflush(stdin) again.....
why the using namespace std when using the old headers?
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi