Hello, this is my first time here, although I have hit this website many times on google looking for quick fixes to my problems. I am a student in high school and I persuaded my teacher to let me study c++ (as opposed to 3ds max). I have been working with c++ now for maybe a month, and I'm having trouble with some code I wrote the other day, It doesn't execute the way I want it to, I'd appreciate some help - (I'm sure the solution is some simple thing I just don't know - all my experience comes from the tutorial on cprogramming.com...).
So here is my code:

#include <iostream>
using namespace std;

int main () {

	int cinuser;

	cinuser=1;

	while(cinuser==1){ 

//this is here so that if the username is wrong I can rerun the code

		char username [11];

		cout << "Please enter your username. No longer than 10 characters.\n";

		cin.getline ( username, 11, '\n' );

/* here is where the user inputs the username he wants... */

		cout << "The username you entered was " << username <<endl;

		cout << "Is this the username you wanted?\n Enter y for yes, and n for no. q to quit.\n";

		char wut;

		cin >> wut;



/*okay, so the user inputs y or n or q. the ASCII codes for y and n are 121 and 110 respectively - comp checks and reacts based on what it is...*/		



			if (wut == 121){

			cout << "I'll do something with this part of the program later or something...";

			cinuser=0;

			}

			else if (wut == 110) {

			cinuser=1;

			}

			else {

			cinuser=0;

			}

	}

}

Anyways, as you can see, I have a program that asks the user to input a username which it stores into a string. I'm using Microsoft's Visual C++ express edition to compile and create the file (I run linux with g++ at home, but at school we use windows). So anyways, the code runs fine and perfect. when the user enters yes or quit that is.
When the user enters n, however, the while loop re-executes itself, and displays:

Please enter your username. No longer than 10 characters.
The username you entered was
Is this the username you wanted?


as I hope you can see, the problem is that it doesn't stop to allow the user to input a username the second (or third or fourth etc) time assuming the user presses n. Now, I theorize that this is because when the user inputs the username the first time, it inputs the username into memory - so what i would have to do is find the block of memory where the username is inputed, and erase what's already in the block of memory there before the while statement executes itself again. However, I have no idea how to do that or even if I'm actually right.

So I must turn this problem over to more experienced programmers.
Also, any advice you'd be able to give to a newbie is always welcome.

Thanks

according to me if u wanna find the place where string is stored u can cin the string in an array and then reinitialize it with zero everytime. n u have forgotten to write the return 0;statement also.

Look closely at line 26. You read one character, whereas user entered two of them: don't forget that she hit enter. This enter is consumed at line 18 next time around, which results in an empty username.

PS: do not use ascii codes in comparisons.

Why don't you use real strings and getline() instead of char arrays and cin.getline()?

Look closely at line 26. You read one character, whereas user entered two of them: don't forget that she hit enter. This enter is consumed at line 18 next time around, which results in an empty username.

PS: do not use ascii codes in comparisons.

Well, I'll be damned. You're a genius. I would have never have realized that.

cout << "Is this the username you wanted?\n Enter y for yes, and n for no. q to quit.\n";

		char wut [2];

		cin.getline ( wut, 2, '\n' );

This should fix that right?

And is there a reason I shouldn't use ascii codes in comparisons? it worked when i compiled...

Why don't you use real strings and getline() instead of char arrays and cin.getline()?

don't know how to use those yet - although I should get on that.

To compare a character value, use the character. Single quotes around a single character is what you need: if (chr == 'y') And remember, 'Y' and 'y' are not the same values...

For comparison, double quotes go around a string (or multiple characters): "This is a string"

Well, I'll be damned. You're a genius. I would have never have realized that.

I am not a genius. The difference between us is that you have a month of experience, and I have 30 years. Just keep in mind that mental debugging is only allowed to those who reach the third level of enlightenment. Until then you must use a debugger.

undefined symbol username...
your condition is not ture.
while(intuser==1) is always true..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.