-
Array Problem
I am learning arrays and I have a simple question.
Why isnt my do-while loop working?
Code:
#include <iostream.h>
int main()
{
char string[100];
do{
cout << "What is your name?";
cin.getline (string, 100);
cout << "\nHello" << string <<".\n";
cout << "Where are you from?";
cin.getline (string, 100);
cout << "\nYour from" << string <<"! Cool me too\n";
cout << "What is your favorite sports team";
cin.getline (string, 100);
cout << "\nThe" << string <<"! Nah they suck";
}
while ( (string != "BYE" ) && (string != "bye" ) );
return 0;
}
You can see the loop in it .
can you guys please help me
-
try this instead:
Code:
#include <iostream> //more standards-compliant
#include<cstring> //for strcmpi()
using namespace std; //more standards-compliant
int main()
{
char string[100];
do{
cout << "What is your name?";
cin.getline (string, '\n', 100); //stops when the user presses enter or when it reaches 100 chars, whichver comes first
cout << "\nHello" << string <<".\n";
cout << "Where are you from?";
cin.getline (string, '\n', 100); //stops when the user presses enter or when it reaches 100 chars, whichver comes first
cout << "\nYour from" << string <<"! Cool me too\n";
cout << "What is your favorite sports team";
cin.getline (string, '\n', 100); //stops when the user presses enter or when it reaches 100 chars, whichver comes first
cout << "\nThe" << string <<"! Nah they suck";
}while ( strcmpi(string,"BYE")!=0); //compares what's in string to "BYE" in all cases (BYE,BYe,ByE,Bye,bYE,bYe,byE,bye)
return 0;
}
-
>You can see the loop in it
Not as clearly as I could if you used better indention.
>Why isnt my do-while loop working?
Because you can't compare C-style strings with relational operators. The result will not be what you're expecting. Include <cstring> and use strcmp instead:
Code:
while ( strcmp ( string, "BYE" ) != 0 && strcmp ( string, "bye" ) != 0 );
Of course, you can avoid two calls by writing your own case insensitive comparison function. It's easy to do. Or if your compiler supports something like stricmp you can use that instead.
-
>while ( !strcmpi(string,"BYE"));
This won't do what you're expecting and certainly not what the OP's intention was.
-
>>This won't do what you're expecting and certainly not what the OP's intention was.
sorry about that... fixed... I think... it did what I was expecting, but I wasn't paying enough attention to what the OP wanted...
-
Those arent working correctly.
Heres the code with the loops.
Code:
#include <iostream>
#include <cstring>
using namespace std
int main()
{
int age;
char string[100];
do{
cout << "What is your name? ";
cin.getline (string, 100);
cout << "Hello " << string <<".\n";
cout << "How old are you? ";
cin.getline (string, 100);
age = atoi (string);
cout <<"\n" << string << ", Really!?!?! You're that old.\n";
cout << "Where are you from? ";
cin.getline (string, 100);
cout << "\nYour from" << string <<"! Cool me too";
cout << "\nWhat is your favorite sports team";
cin.getline (string, 100);
cout << "\nThe " << string <<"! Nah they suck";
}
while ( strcmp ( string, "BYE" ) != 0 && strcmp ( string, "bye" ) != 0 );
return 0;
}
-
>Those arent working correctly.
That's because you wanted to use a do..while loop. You would need to add another conditional after getting the name to check and see if it's an exit condition. Or you could use a simpler while loop and a redundant output call:
Code:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char string[100];
cout << "What is your name? ";
while ( cin.getline (string, 100)
&& strcmp ( string, "BYE" ) != 0
&& strcmp ( string, "bye" ) != 0 )
{
cout << "Hello " << string <<".\n";
cout << "How old are you? ";
cin.getline (string, 100);
cout <<"\n" << string << ", Really!?!?! You're that old.\n";
cout << "Where are you from? ";
cin.getline (string, 100);
cout << "\nYour from " << string <<"! Cool me too";
cout << "\nWhat is your favorite sports team ";
cin.getline (string, 100);
cout << "\nThe " << string <<"! Nah they suck\n";
cout << "What is your name? ";
}
return 0;
}
Our suggestions were correct, they just weren't what you wanted. We didn't give you what you wanted because you didn't ask for it.
-
that only works in the first cin.getline
I want ot be able to say bye and exit the program at any point
-
test it every time you get new input, then use break;
-
thatll be alot of code.
]is there any other way to do it.
I don't know why the do..while isn't working
-
>thatll be alot of code.
Sorry.
>is there any other way to do it.
No, not really. If you want to scatter interactive prompts and input calls all over the place you have no choice but to test each one individually.
>I don't know why the do..while isn't working
I'm going to start ignoring you unless you stop saying "it's not working" and tell us what you think it should be doing in no uncertain terms.
You tell us the problem, we give you an answer, you say it doesn't work. Then you go on to tell use you really wanted something different than the question you asked. When you don't like the answer we give, you revert to your original unhelpful question. Before you write code and ask questions, figure out what you really want, because this circular question and answer deal is annoying.