Two outputs instead of one

Pages: 12
I made a C++ program(link here: https://replit.com/@nikegamerjjjj/NikPoloTerminal?v=1).
I have a problem. You can see in link code, so when you are there, my problem is following: When I wrote username and password and logged in, in beginning it writes two times: polo - “username” >>
But it should print one time when I logged in as I have only one cout command.
Help!
Post your code.
I said it lies in link. You can both run it and see code by clicking the code section.
Here is the computer program found at the linked website
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>

using namespace std;

void space() {
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
}

int main()
{
  cout << "NikPolo Command Terminal \n v.0.1.3" << endl;
    string user, pswrd, checkuser, checkpswrd;
    
    bool whileterminal = true;
    bool while_login = true;
    bool while_pswrd = true;
    bool while_user = true;

while (while_user) {
  cout << "Write new username: ";
  cin >> user;
  cout << "Save username? Write yes or no: ";
  string yesno;
  cin >> yesno;
  if ("yes" == yesno or "y" == yesno) {
        while_user = false;
        cout << "Saved!" << endl;
  }
  else {
    continue;
  }
}
while (while_pswrd) {
  cout << "Write new password: ";
  cin >> pswrd;
  cout << "Save password? Write yes or no: ";
  string yesno2;
  cin >> yesno2;
  if ("yes" == yesno2 or "y" == yesno2) {
    while_pswrd = false;
    cout << "Saved!" << endl;
  }
  else {
    continue;
  }
}
  
space();
  while (while_login) {
    cout << "Login: " << endl;
    cout << "Write username: ";
    cin >> checkuser;
    cout << endl;
    cout << "Write password: ";
    cin >> checkpswrd;
    cout << endl;
    if (checkuser == user) {  
      if (checkpswrd == pswrd) {
            space();
            space();
            while_login = false;
            cout << "Login Complete!" << endl;   
 }
      else {
          cout << "Incorrect password or username" << endl;
          cout << endl;
 }   
   
 }
    else {
       cout << "Incorrect username or password" << endl;
       cout << endl;
                    
}
                
}
   while (whileterminal) {
     cout << endl;
     string commands;
     cout << "polo - " << user << " >> ";
     getline(cin, commands);
     if (commands == "exit") {
       cout << endl;
       cout << "Confirming exit..." << endl;
       system("pause");
       whileterminal = false;
     }
     else if (commands == "") {
       continue;
     }
   }
}
Last edited on
Using the line numbers in mbozzi's post:
Line 6-80: Have you never heard of a loop?
It never writes just "username" as you say. What line is repeated?
It repeats this: polo - “any username” >>

And it shouldn’t repeat. Only when you wrote a command it should do something and write that again, but I never wrote any commands and it still duplicates in beginning
We prefer you to post your code/outputs here. Links that will be later removed are no good to anyone.
It repeats this: polo - “any username” >>


Because you're mixing stream extraction (>>) with getline(). >> doesn't extract the terminating white-space char (\n in the case) which getline() then gets and uses as input.

Somewhat simplified:

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <string>
#include <iomanip>

using std::cout;
using std::cin;

void space() {
	const static std::string lines(76, '\n');

	std::cout << lines;
}

int main()
{
	cout << "NikPolo Command Terminal \n v.0.1.3\n";
	std::string user, pswrd, checkuser, checkpswrd;

	for (bool while_user {true}; while_user; ) {
		cout << "Write new username: ";
		cin >> user;

		std::string yesno;
		cout << "Save username? Write yes or no: ";
		cin >> yesno;

		if ("yes" == yesno or "y" == yesno) {
			while_user = false;
			cout << "Saved!\n";
		}
	}

	for (bool while_pswrd {true}; while_pswrd; ) {
		cout << "Write new password: ";
		cin >> pswrd;

		std::string yesno2;
		cout << "Save password? Write yes or no: ";
		cin >> yesno2;

		if ("yes" == yesno2 or "y" == yesno2) {
			while_pswrd = false;
			cout << "Saved!\n";
		}
	}

	space();

	for (bool while_login {true}; while_login; ) {
		cout << "Login:\n";
		cout << "Write username: ";
		cin >> checkuser;

		cout << "\nWrite password: ";
		cin >> checkpswrd;
		cout << '\n';

		if (checkuser == user && checkpswrd == pswrd) {
			space();
			space();
			while_login = false;
			cout << "Login Complete!\n";
		} else
			cout << "Incorrect password or username\n\n";
	}

	for (bool whileterminal {true}; whileterminal; ) {
		std::string commands;

		cout << "\npolo - " << user << " >> ";
		std::getline(cin >> std::ws, commands);

		if (commands == "exit") {
			cout << "\nConfirming exit...\n";
			//system("pause");
			whileterminal = false;
		}
	}
}

Can you write this code again seeplus, so it using namespace std, as I don’t understand much of the code because of std::
seeplus.

So I what I need to change in code, to stop it doing that?
Can you write this code again seeplus, so it using namespace std, as I don’t understand much of the code because of std::


Why not? What's not to understand between:

 
string mystr;


and

 
std::string mystr;


The first looks for string in the namespace(s) specified in using statements. The second explicitly says to use the std namespace version.
Last edited on
seeplus, did you mean string instead of sting in the first code snippet?

I know I make lots of typos myself.
seeplus, I repeat again, what do I need to fix in it? Do
seeplus, did you mean string instead of sting in the first code snippet?

I know I make lots of typos myself.


Yep. Changed..
seeplus can you answer me? Question is on top of yours
seeplus, I repeat again, what do I need to fix in it? Do


Fix what? Not using namespace std is not something to fix. The code as provided compiles OK and AFAIK works as expected based upon the original.
Man, don’t get aggressive….I just asked. So what do I need to change in my code(I don’t want change whole thing as it used time to write all…) ? Do I need to have: “cin >> ws, commands” instead of “cin, commands” in getline()?
Our previous posts crossed.

Do I need to have: “cin >> ws, commands” instead of “cin, commands” in getline()?


If you have using namespace std at the top, then you don't need std:: in front of the library functions, classes etc. Having std:: though is good practice.

Yes - as that removes the white-space left by the >> extraction before getline() extracts its data.
Last edited on
Pages: 12