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.
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.
#include <iostream>
#include <vector>
#include <sstream>
usingnamespace 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(unsignedint 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;
}
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>.