Console window closes

I would like my program to be able to save input then make it possible for the user to make an input again and see if it matches the saved input?

what am i doing wrong??

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
27
 #include <iostream>
using namespace std;

int main() {
int createdUN;	
int realUN;
int guessedUN;

createdUN=realUN; 
{

	cout<<"create a username:  "<<endl;
	cin>>createdUN;  
}
while (guessedUN != realUN){ 
	cout<<"Input username"<<endl;
	cin>>guessedUN;
	
	if (guessedUN != realUN ){cout<<"wrong username! try again. "<<endl;
	}
	else if (guessedUN = realUN){
		cout<<"Correct Username!"<<endl;
	}
}
		system ("PAUSE");
		return 0;
}
I've made some comments in your code below.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;

int main() 
{
    //are usernames just going to be numbers? Usually I'd think of a username being a string, not an int.
    int createdUN;	
    int realUN;
    int guessedUN;

    //realUN has not been initialized to any valid value, not sure what you mean to do here
    createdUN=realUN; 
    
    {
    	cout<<"create a username:  "<<endl;
    	cin>>createdUN;  
    }
    //neither guessedUN or realUN have been initialized with a valid value, this isn't comparing valid values
    while (guessedUN != realUN)
    { 
    	cout<<"Input username"<<endl;
    	cin>>guessedUN;
    	
	    //realUN has not been initialized to any valid value to make a comparison here
    	if (guessedUN != realUN )
    	{
    	    cout<<"wrong username! try again. "<<endl;
    	}
    	
    	//realUN has not been initialized to any valid value to make a comparison here
    	//also = is assignment operator, == is equality operator
    	else if (guessedUN = realUN)
    	{
    		cout<<"Correct Username!"<<endl;
    	}
    }
		
	system ("PAUSE");
	return 0;
}
Last edited on
A very simple way to save input between instances of your program is to write something to a file. here is a simple example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    using namespace std;
    const string filename("data.txt");

    ifstream ist(filename);
    string line;
    if (getline(ist, line) && line != "")
        cout << "Saved username: "<< line << '\n';
    else
        cout << "No username was saved.\n";

    cout << "Enter new username: ";
    getline(cin, line);
    ofstream ost(filename);
    ost << line;
}

Obviously, modify this to fit your needs.
Of course it has some flaws (you can enter just whitespace, or nothing).
btw: http://www.cplusplus.com/doc/tutorial/files/
Last edited on
Topic archived. No new replies allowed.