while???

Hi guys, i started learning c++ a few weeks ago, and im getting better everytime, but im stuck with this. I know its probably a very **** thing but i just can figure it out, so here it is:
------------------------------------------
main ()
{
clrscr();
char ans1[15];
char ans2[15];
cout<<"hello, how are you feeling today sir?";
cin>>ans1;
cout<<"would you like some coffee sir? (y/n)";
cin>>ans2;
while (ans2!="y" && ans2 !="n")
{
cout<<"please, answer with (y) or (n)";
cin>>ans2;
}
if (ans2=="s") cout<<"your coffe will be in the office right away";
else cout<<"no problem sir";
guetch();
return 0;
}

the problem is that when i write "s", the program keeps asking me to write (s) or (n). i can write any letter, and the program keeps asking, even when i write s, or n. What am i mising here?

the program keeps asking me to write (s) or (n).

Do you mean (y) or (n)? I don't see anywhere in your code that asks for (s) or (n).

Anyway, since you're learning C++, here are some issues with your code:

main ()
This is not C++ and it never ever has been. Whatever you're using to compile this is bad and you should throw it away and get a modern compiler for free.

clrscr();
This is non-standard. I can only assume you've included a non-standard header that you haven't shown us. Please show all such code in the future.

char ans1[15];
Whilst not wrong, this is C++, and in C++ we use string objects.

ans2!="y"
This shows a fundamental misunderstanding of what you're doing. Here, I suspect you think you are comparing the character you typed in with the character y. You are not; you are comparing a char pointer with another char pointer.

if (ans2=="s")
So hold on, you just told the user to enter either y or n, and now you're immediately testing to see if the letter is s? But you just told the user to enter y or n. What exactly are you trying to do here?

guetch();
I suspect you meant getch();. How did this program even compile?
Last edited on
im using turbo c++ 3, very old one. but is the compiler that my school uses. The idea was to ask the user to write (y) for yes or (n) for no, and that the program keeps asking till user writes one of those. About the code, ye, my teacher sucks at teaching.
anyway, what do you mean with char pointers?
The idea was to ask the user to write (y) for yes or (n) for no, and that the program keeps asking till user writes one of those.

So what's with the if (ans2=="s")? Was that meant to be if (ans2=="y")

what do you mean with char pointers?


Do you know what a pointer is? (I'm going to guess not, but I could have guessed wrongly.)

but is the compiler that my school uses.

My sympathies. You're being taught some kind of mutant ancient pre-standard C++ that would be rejected out of hand by a modern (correct) C++ compiler. There's not much you can do about it, except be aware of it and try to not learn too much incorrect information, I suppose.
Last edited on
i wanted to put "y" not s, and no idea what pointers are, like i said, my teacher learned c++ 30 years ago, and remembers nothing now so he never explained us the basic and more important things,
how should i write this then?
In C++, something like this. What your teacher expects, I have no idea.

Pointers are absolutely fundamental to C and C++ programming and if you want to ever get anywhere, read this:

http://www.cplusplus.com/articles/EN3hAqkS/

and this:

http://www.cplusplus.com/articles/z186b7Xj/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <string>
#include <iostream>

using namespace std;

int main()
{
  string userInput;
  cout<<"hello, how are you feeling today sir?";
  cin >> userInput;

  cout<<"would you like some coffee sir? (y/n)";
  cin >> userInput;

  while( userInput != "y" && userInput != "n" )
  {
    cout<<"please, answer with (y) or (n)";
    cin >> userInput;
  }

  if (userInput == "y")
  {
    cout<<"your coffee will be in the office right away" << endl;
  }
  return 0;
}
According to the Wiki, Turbo C++ 3.0 was released in 1991.

Teaching C++ using a compiler that predates the first standard should be illegal.

But I digress...
If you want the user to answer a yes/no question, you really only need to store their input in a char variable (as opposed to a c-style string char [])
And when comparing single characters, the character literal is put in single quotes, not double ones.
1
2
3
char answer;
cin >> answer;
if(answer == 'y') //notice the ' ' 


now that you mention it, the compiler was created in 1990, so its 22years old, thanks for the code, what free compiler can i use?
According to the Wiki, Turbo C++ 3.0 was released in 1991.


And yet this is far from an isolated incident. It seems so odd, when modern compilers used by professionals are just given away for free.

Coding on windows? Many newcomers go for some express version of Visual Studio.
Last edited on
The two that I use are MinGW (Code::Blocks) and MSVC.
Code::Blocks is competely free and Visual Studio offers a free "Visual Studio Express" version.
While 'm here, I should tell you now that a compiler and an IDE are NOT the same thing. We've been saying compiler, but that's just us being lazy.

The compiler is the command-line tool that takes text files and turns them into binary object files.

The IDE is an (optional) set of tools that (usually) has a compiler in the installation bundle, and is a set of pretty utilities and menus and so forth to make it easier for you to organise your work and pick various compilation options.

Visual Studio - IDE. Comes with the MS compiler.
Code::Blocks - IDE. There is a download option that comes with the MinGW port of the gcc compiler, and there is a download option that comes without a compiler. Code::Blocks contains an option for you to choose which compiler it should use (which you must arrange yourself, if you didn't choose the download option with a compiler).
Turbo C++ - ancient IDE with ancient compiler.
conclusion, i should kill my profesor for teaching so bad and with such an old program...
What would happen if you handed in correct, modern C++? Might be worth a try :)
Conficker97 wrote:
conclusion, i should kill my profesor for teaching so bad and with such an old program...


Kill is such a harsh word. Ignore would be better (and more legal :P) IMO.
There are a myriad of resources available to learn c++: books, websites, videos, etc. Find the one(s) that are right for you, and you'll be good to go.
Last edited on
i could hand in the correct c++ code, but i would need to re-learn c++ again, all the ideas in my head are from a 22 year old program, tomorrow is the test, i will try to do my best (with the tools this school give).
anyway, tomorrow im going to start learning from the pdf and documentation on c++ of this page. maybe the teacher has to learn from his students sometimes...
closed account (ypfz3TCk)
All i would say is that you should normally use the string type for c++ instead of the c style array of char. It is a lot easier to understand the code - amongst other reasons.
Topic archived. No new replies allowed.