Reading extra data in C++ [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a file that consists of data points, and numbers associated with those data points. For example, the file looks something like:
(1,2) 45
(3,4) 12
(23,9) 6 90
(3,5) 4 8
For each data point, I want to set variables "int num1" and "int num2." As you can see, sometimes I have to read an extra number attached to my data point. When there is no extra number, I set num1 and num2 to the value given. I have no problem getting the x and y from the coordinate, but I'm not sure how to check to make sure I'm getting both numbers. I feel like I have to use getline(), but I'm not sure where to go from there. The file is saved as "ins."
char parentheses1, parentheses2, comma;
int x, y, num1, num2;
ins >> parentheses1 >> x >> comma >> y >> parentheses2;
ins >> num1 >> num2;

Take Option 2 from this answer as a basis for your code.
Read from the stringstream into x and y
iss >> parentheses1 >> x >> comma >> y >> parentheses2;
and then gather numbers until there are none left on the line
std::vector<int> numbers; // dynamic array of numbers
int temp;
while (iss >> temp) // exits as soon as you can't read an int from the stream
{
numbers.push_back(temp); // store in vector
}
You can simplify the above a bit if there are never more than, say, 2 numbers.

Related

C++: How to find the sum of a series of numbers separated by spaces

I am trying to create a programme taking two lines of imput, say
n
m1, m2, m3, ... , mn
where 1, 2, 3 ... n are all subscipts, so that n is the total number of integers that will be entered, and m1...mn are the integers themselves. Now, I want to find a way to sum m1 to mn up.
For example, if the user entered 5 as the first input line, we know that the user will input 5 numbers separated by space, but in the same line, say 12 14 17 19 28, the expected output should be 90. I understand that to take in inputs we need to use cin >> a >> b >> c >> d >> e after all the five variables have been defined. However, I am unable to find a way to solve it as the input on the first line is not always 5, and if it's 1000, it is not realistic to write cin >> a >>b ... 1000 times. Anyone can help? Thanks!
You said in comments:
If different lines I can still use for loop + cin. But single line when the quantity is not confirmed is hard :(
You can use a for loop regardless of whether the numbers are on a single line or on different lines. operator>> skips leading whitespace, and then stops reading on whitespace. Space and NewLine characters both count as whitespace. So, the following will work just fine (error handling omitted for brevity):
int n, num, sum = 0;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> num;
sum += num;
}

Wrong type of input [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have the code below, so basically my problem is: when someone input a character like 'a', it will pop a message that requires a re-input
I tried using the ASCII:
if (a >= 97 && a <= 122) but it still didn't work
double a;
cin >> a;
if (a >= 'a' && a <= 'z')
{
cout << "Wrong input, please re-input a: " << endl;
cin >> a;
}
cout << a;
I expect it to pop the message to re-input but the actual output is always 0 no matter what character I input
The state of a stream can be checked by using it directly in a condition. If all is okay it "returns" true, otherwise "false". So you can do e.g.
if (!(cin >> a))
{
// Invalid input, or other error
}
On invalid input you need to clear the state.
Note that if the input is invalid then the input will not be read, and the next time you attempt to read you will read the exact same input that failed the first time. One way to solve it is to ignore the rest of the line. Another is to read a whole line into a string that you then put into an input string stream for the parsing of the input.

One Line of Input to Multiple Variables [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to take one line of input separated by spaces and assign it to four variables.
I read that you're meant to be able to do:
#include <iostream>
#include <string>
int main()
{
int i, n1, n2;
std::string s;
std::cin >> i >> s >> n1 >> n2;
}
For input: 12345 string 4 5 the result would be i = 12345, s = string, n1 = 4, n2 = 5.
But I have to press enter for each variable. I need one line to assign to the four variables.
This code works already. Compile and run it, and then when the program waits for input just type in:
"12345 string 4 5", enter and you're done.
From this online CPP tutorial:
Extractions on cin can also be chained to request more than one datum in a single statement:
cin >> a >> b;
This is equivalent to:
1 cin >> a;
2 cin >> b;
In both cases, the user is expected to introduce two values, one for variable a, and another for variable b. Any kind of space is used to separate two consecutive input operations; this may either be a space, a tab, or a new-line character.

Efficient way to count ints in line C/C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I've been tasked with solving a rather simple problem but I'm stuck on 1 thing
Given the following file structure:
4
124 123 145 152
1400
13 23 51 24 1412 5151 52512 12412...
...
I am supposed to do various tasks with data but before doing anything with it I have to check whether int in first line corresponds to number of ints in 2nd line (same for 3rd and 4th line)
The problem is the number of elements can reach up to 150 000 and int value is between (1;2 000 000) so using getline can be problematic (afaik) as it will be resource heavy.
Cin will ignore whitespaces and \n so I will never know when line ends
I can't modify the original file and I try to use as least resources as possible.
hm not sure if i understand the question completely but why dont you use getline to store the line that contains 1400 ints as a string. Afterwards using a istringstream make sure you read 1400 ints. Maybe something like this:
getline(cin, line);
while(iss >> n && i++ < numOfInts) {}
then outside make sure i == numOfInts.
This way you'll never actually be storing 1400 ints, you'll just keep overwriting the previous integer
You can create a custom function to take the integer values, Something like this:
int getints(){
int ctr = 0;
char ch;
int sign = 1, n=0;
while(ch!='\n'){
while(ch==' ' || ch=='\t')
ch = getchar();
if(ch=='-'){
sign = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
n = n*10 + ch - '0';
ch = getchar();
}
n *= sign;
++ctr;
}
return ctr;
}
This function will return the number of integers scanned in a line. Just check it with the number of integers that are supposed in a line. It basically skips all whitespace characters and terminates input after newline is found.

No matching function for call to error when reading from a valid file [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have the following data in a file:
0001 O 100 102.30
0001 O 101 333.22
0001 O 102 679.13
0001 P 103 513.36
0001 P 104 700.94
With the following code:
vector<string> customerID;
vector<char> transactionType;
vector<string> transactionNumber;
vector<double> amount;
string cID, tT, tN, amnt;
for(;infile2 >> cID >> tT >> tN >> amnt;){
customerID.push_back(cID);
transactionType.push_back(tT);
transactionNumber.push_back(tN);
amount.push_back(amnt);
}
and the error:
error: no matching function for call to 'std::vector<char>::push_back(std::string&)'
error: no matching function for call to 'std::vector<double>::push_back(std::string&)'
Is it assuming every data item is a string ?
How can i fix this ?
Yes, it is. You're reading four strings, and you're converting any of them to anything else. C++ doesn't provide implicit conversions from std::string to non-string types.
The easiest way to fix your problem is to read tT and amnt as char and double. Simply declare your variables as
std::string cID, tN;
char tT;
double amnt;
and it should work. Alternately, you can read them as strings and convert them.
As written, you are using four string variables when reading. You can correct it by declaring variables of the appropriate types.
vector<string> customerID;
vector<char> transactionType;
vector<string> transactionNumber;
vector<double> amount;
string cID, tN;
char tT;
double amnt;
for(;infile2 >> cID >> tT >> tN >> amnt;){
customerID.push_back(cID);
transactionType.push_back(tT);
transactionNumber.push_back(tN);
amount.push_back(amnt);
}