cin.get() Not working as pause. Do_while loop

Hi! Pardon my n00bness =)

Im working on a school assignment, and have been trying to avoid system("pause") as I have come a cross a lot of hate for system("anything"), and wanting to learn good practice from the beginning.

Anyhow, I cant get cin.get() or cin.ignore() to work as a pause in my latest program. The program basically inputs resistor values and adds them in paralell, until a 0 is typed, then it starts a new paralell branch and ad the serial resistance. windows.h is included because i wantet to change some text color.

Sorry for the norwegian commenting.

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
#include<iostream>
#include<windows.h>

using namespace std;


int main()
{
	double motstand=1;						//Resistor verdi. Satt til 1 for å starte første if funksjon
	double paralleli = 0, parallelr = 0;	//Parallel induktans og resistans
	double serie=0;							//Total resistans parallelgrener i serie

	cout << "Program som beregner total resistans av paralellkoblede motstander i serie\nR=0 avslutter en paralellgren\nR<0 angir total resistans og avslutter programmet\n\n";
	cout << "Starter f" << char(155) << "rste paralellgren";	//Heia norske bokstaver!
	cout << endl << endl;
	
	do{
		cout << "Tast inn resistorverdi\n";
		cin >> motstand;
		cout << endl;

		if (motstand > 0){					//Beregner ekvivalent resistans i parallellgren			
			paralleli = paralleli + 1 / motstand;
			parallelr = 1 / paralleli;
			cout << "Ekvivalent resistans\t\t: " << parallelr << "ohm" << endl << endl;
		}		
		if (motstand == 0){					//Fullfører parallelgren		
			serie = serie + parallelr;			

			cout << "node, " << endl;
			cout << "Forel" << char(155)<<"big sum\t\t\t: " << serie << "ohm" << endl;
			cout << "Starter ny parallelgren" << endl << endl;
			
			paralleli = 0;					//Tilbakestiller verdier før neste paralellgren
			parallelr = 0;					//
			motstand  = 1;					//Setter motstand til 1, klar for neste paralellgren
		}
		if (motstand < 0){
			serie = serie + parallelr;

			cout << "Total resistans\t\t\t: " << serie << "ohm" << endl << endl;			
		}
	} while (motstand >= 0);
	
	cout << "Trykk enter\n";
	cin.get();
}


Whenever a value <0 is typed the program quits imediately unless using system("pause"). The rest of the program is doing what it should (I guess it could be more elegant though =P)
Last edited on
Let's say you input variables like this:
1
2
int x;
std::cin >> x;
There's multiple problems with this, but the one you are running into is that the formatted input operator >> will read the number, and then leave the rest of the stream alone. That means that if there was anything after the number, it will still be there later. So, in your case, the newline character is still in the input stream.

I recommend reading entire lines of input at a time and then going through the line as needed:
1
2
3
4
5
6
7
8
9
10
11
12
std::string line;
std::getline(std::cin, line); //read entire line
std::istringstream in {line};
int x;
if(in >> x)
{
    //success: continue with program
}
else
{
    //error: try again - std::cin is not affected and is safe to re-use
}
By reading entire lines at a time, you know that it is always safe to read from std::cin without worrying about extra characters.
Last edited on
Topic archived. No new replies allowed.