While loop

so im doing this exercise in a book i recently purchased and i can't figure out how to do it. actually what it is trying to tell me to do. It says to write a program that consists of a while-loop that (each time around the loop) reads in two ints and then prints them. Exit the program when a terminating '|' is entered.

No clue to what its trying to say.

Can anybody help me?
Thanks.
First of all are there any examples of while loops in your book? There should be plenty of examples within that chapter. Basics for your while loop would consist of:

1
2
3
4
5
6
7
8
9
10
11
12
13
while (n != "l")
{ 
     cout << (something to ask the user for input)
     cin >> (some variable)
     cout << (same message as before)
     cin >> (some variable)
     cout << (two variables that hold the integers)
     
     // pseudocode
     now ask the user if they want to quit
     if they enter "l"
     then the while loop will quit
}


Try analyzing your problem first before you write any code. Its best to write pseudocode first to analyze and setup the concept of solving the problem first. Then you can worry about syntax later. The concept of your problem is just to check each time for "l", and until the user enters that in, keep asking the user for two integers and then print them out.
Heres what i got so far. at the end, if you dont enter | then it just sits there and waits for you to enter a number first. it wont prompt the user again. can u help me so it prompts the user for input without having to enter a number to get it to do it? Thanks.

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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
inline void keep_window_open() {char ch; cin >> ch;}

// Chapter 4 Drill

int main()
{
	int val1;
	int val2;
	char n;
	while (n != '|')
{
	cout << "Enter two intergers:\n";
	cout << "Interger 1: ";
	cin >> val1;
	cout << "Interger 2: ";
	cin >> val2;
	cout << val1 << '\t' << val2;
	cout << endl;
	cout << "Enter | if you want to quit: ";
	cin >> n;
	}
}
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 <vector>
#include <sstream>

using namespace std;

int main()
{
	vector<int> myInts; //vector that will contain the ints entered
	vector<string> input; //string that will contain the string form of the ints entered
	stringstream myStream;
	int i = 0;
	
	while(1) //we want to loop forever 
	{
		for(int i = 0; i < 2; i++) //we want to loop twice to read in each int
		{
			input.push_back("");  //add a dummy value so that input[i] can exist

			cout << "Please enter integer number " << i+1 << " or | to exit" << endl; //ask for the number and store it in input[i]
			getline(cin, input[i]);
			
			if(input[i] == "|") //if | is entered instead of a number exit the program immediately
				exit(1);
			
			myInts.push_back(0); //we want a place holder to be able to put the ints in myInts[i] later
		}
		
		cout << "The numbers you entered were:\n";
		
		for(unsigned int i = 0; i < input.size(); i++)
		{
			myStream.str(input[i]); //the next two lines are required to convert the string input[i] to an integer and to store it in myInts[i]
			myStream >> myInts[i];
			cout << myInts[i] << endl;
			myStream.clear(); //myStream.str will not work for another input unless we clear it of the old one first
		}

		input.clear(); //clear our vectors so we can read in our next two ints
		myInts.clear();

		
	}
		
	system("pause");
return 0;
}

Last edited on
translore thats a great solution but I think it might be a little too much for what tlittle asked for. Anyways, tlittle if you can follow the code that translore wrote he used an advanced array called a vector, and basically instead of having the user enter in integers and then the exit digit, you can just have the user enter it in the vector and then check it just once instead of multiple times.

I don't know how advanced you are but that is pretty much the jist of what translore wrote. Also, if you are not going to use functions from certain header files you don't need to include them. For instance you could have done everything with just <iostream>.
Topic archived. No new replies allowed.