Skip to main content
GameDev.net gamedev.net
🔒 Locked

help for begginer

Started by burgess_1984 Feb 27, 2006 at 10:23 AM 10 replies 1.6k views
Original Post
burgess_1984
burgess_1984
Im confued a little, I need to check if the user has inputted a Y or N character and I wrote this to check but i keep getting an error. please help. if (dataBool == y) { ofstream fout (dataName); cout << "\nData saved to " << dataName<< " file."; } the basic idea is that if they press Y i will save some data to a file. I cin >> dataBool on the line preceding this. cheers
cNoob
cNoob
What is the error message burgess.
burgess_1984
burgess_1984
y undeclared, first use this function.

I want to know if the char Y is in the variable , as in Y or N.

..

ps good luck with that OS.
cNoob
cNoob
try putting if(dataBool == 'Y') that might get rid of one error.
Bob Janova
Bob Janova
if(dataBool == "Y"){ ... }
swiftcoder
swiftcoder
Use 'y' instead of y.
y is a variable or function name, 'y' is the character y.

Ah, beaten to it
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
burgess_1984
burgess_1984
Fixed. So dumb of me, thank you.
burgess_1984
burgess_1984
OK, now Im having another problem..

If I use a char in the IF statement the programme doesnt act on it, whereas if I use an integer the programme responds

ie

cout << "Is this data correct 1/0\n";
int dataBool;
cin >> dataBool;

if (dataBool == (1))
{

cout << "\nData saved to " << dataName<< " file.";
}

in this case the programme displays " Data saved to........."

***************************************************

cout << "Is this data correct Y/N\n";
char dataBool [1];
cin >> dataBool;

if (dataBool == "y")
{

cout << "\nData saved to " << dataName<< " file.";
}

in this case my programme ends, i assume i can use any data type in an IF statement.
simon10k
simon10k
Use 'y' not "y"

"y" is a null terminated string.
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
burgess_1984
burgess_1984
ISO C++ forbids comparison between pointer and integer
Zahlman
Zahlman
Please, please don't try to handle textual data like that. Throw out any tutorial or reference which tells you to.

cout << "Is this data correct Y/N" << endl;string isCorrect; // need to include <string>; 'string' is in the std namespace// just like 'cout' is.cin >> isCorrect;if (dataBool == "y") { // this comparison now works  cout << "\nData saved to " << dataName<< " file.";}


The problems with reading into a character array are many - but the two that will bite you right off the bat are:

1) You need one more character than there is text (in order to hold a "null terminator"), and not only that, you need one more character *than you read in*, and you don't even know ahead of time how much that will be. A std::string resizes itself during the reading-in process automatically as needed.

2) You simply can't compare their contents that way. You end up comparing pointers, i.e. locations in memory where the input is located versus where the constant "y" is located. These are obviously never the same.

By the way, reading into either a string or a character array in this way will only read one word (i.e. something separated by space, tab or other whitespace). If you want a whole line, you should use std::getline():

getline(cin, isCorrect);if (isCorrect == "Yes, that's right.") {  // This actually has a chance of executing}
burgess_1984
burgess_1984
Yes your right, the reason I had done this wasnt because of a tutorial but because I assumed I could do it. Earlier in the programme I had done this :-

if (input == 1)
{
system ("cls");
newData();
}

I assumed that because it worked with an integer it would work with CHAR. Maybe I picked up this habit at college when I used Visual Basic.

Thank you for taking the time to explain the problem and give some sample code.

chris

[Edited by - burgess_1984 on March 2, 2006 12:44:30 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.