Hello everyone! I am having trouble stopping user input with a different variable type. In my case, the user input are numbers, but the problem specifies that I should stop the input stream with a '|' character. Below is my code with comments:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <complex>
#include <ctime>
#include <fstream>
#include <vector>
usingnamespace std;
int main()
{
vector<int>v;
int number;
int N;
int sum = 0;
cout << "Pleae enter some numbers (press '|' to stop): \n";
while(cin >> number)
{
//if( "something" == '|')
//{
// break;
//}
// Here for the "something", can I use the getline function? I tried cin.getline(number,1), but unfortunately,
// the character '|' is not an integer type. Could you help me please? Thank you!
v.push_back(number);
}
cout << "Now please enter how many numbers you wish to sum, starting from the first: \n";
cin >> N;
for(int i = 0; i<N; i++)
{
sum += v[i];
}
cout << "The sum of the first " << N << " numbers: ";
for (int j = 0; j< N-1 ; j++)
{
cout << v[j] << ", " << endl;
}
cout << "and " << v[N-1]<< " is " << sum << "." << endl;
}
So what should I do to use '|' instead of an integer to stop the user input? Thank you in advance for your help!
I also tried your while loop approach just now, but the program does not allow me to enter a second number after I typed my first one, and the program is literally dead.
Hi JLBorges! Thank you for your reply. Could you also give your advice on what I should write to replace the "something" in my if statement? Thank you! I tried cin.get(), cin.peek(), but they all made my program crash after I entered the '|'. I don't know what to do...
I'll get input to a string buffer and determine if that string buffer is a number, or contains '|' char or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
while (true)
{
char inputBuffer[20];
cout << "Enter a number ( | to stop): ";
cin >> inputBuffer;
if (isint(inputBuffer))
{
int tempNumber = atoi(inputBuffer); //include stdlib.h if you can't use atoi()
//append tempNumber to vector...
}
elseif (hasStopChar(inputBuffer, '|'))
{
break;
}
//input is not a number and doesn't have stop char -- nothing to do here...
}
1 2 3 4 5 6 7 8
bool isint(constchar* str)
{
for (constchar* ptr = str; *ptr; ++ptr)
{
if (!isdigit(*ptr)) returnfalse; //include ctype.h if you can't use isdigit()
}
returntrue;
}
Hello LowestOne and tntxtxt! Thank you for your replies!
I am just a beginner in C++ so is there a simpler way of doing this? Actually I just looked up the terminologies. I think my problem is that I want to specify my very own "End-Of-File (EOF)" operator ( the '|' character in this case ) other than the default Ctrl + Z command in Windows. Hopefully there is a simpler way? I appreciate your further attention!
> I think my problem is that I want to specify my very own "End-Of-File (EOF)" operator
> ( the '|' character in this case ) other than the default Ctrl + Z command in Windows.
> Hopefully there is a simpler way?