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