Trying to use int in getline
Asked Answered
A

5

8
cout << "How many questions are there going to be on this exam?" << endl;
cout << ">>";
getline(cin, totalquestions);

This small piece of code comes from a function in a class that I have created and I need totalquestions to be an int so that it can run through a for loop and keep asking the total amount of questions that I have asked.

question q;
for(int i = 0; i < totalquestions; i++)
{
    q.inputdata();
    questions.push_back(q);
}

Where does this piece of code comes to play? Does anyone have any idea to make this work?

Abby answered 30/4, 2011 at 20:3 Comment(2)
Please specify what you think is not working.Cummine
@ulidtko, actually, for this question, it was pretty easy to see what was not working...Wrecker
N
12

Use

cin >> totalquestions;

Check the errors too

if (!(cin >> totalquestions))
{
    // handle error
}
Neolatin answered 30/4, 2011 at 20:6 Comment(1)
@OlayemiIbrahim https://mcmap.net/q/333496/-how-does-ifstream-39-s-eof-work Thanks for contributing anyways!Neolatin
B
5

getline reads an entire line as a string. You'll still have to convert it into an int:

std::string line;
if ( !std::getline( std::cin, line ) ) {
//  Error reading number of questions...
}
std::istringstream tmp( line );
tmp >> totalquestions >> std::ws;
if ( !tmp ) {
//  Error: input not an int...
} else if ( tmp.get() != EOF ) {
//  Error: unexpected garbage at end of line...
}

Note that just inputting std::cin directly into totalquestions does not work; it will leave the trailing '\n' character in the buffer, which will desynchronize all of the following input. It's possible to avoid this by adding a call to std::cin.ignore, but this would still miss the error due to trailing garbage. If you're doing line oriented input, stick with getline, and use std::istringstream for any necessary conversions.

Brae answered 30/4, 2011 at 23:48 Comment(0)
W
3

Do this:

int totalquestions;
cout << "How many questions are there going to be on this exam?" << endl;
cout << ">>";
cin >> totalquestions;

Getline is meant for grabbing chars. It can be done with getline(), but cin is much easier.

Wrecker answered 30/4, 2011 at 20:5 Comment(0)
C
2

One of the better ways of getting an int from user :-

#include<iostream>
#include<sstream>

int main(){
    std::stringstream ss;

    ss.clear();
    ss.str("");

    std::string input = "";

    int n;

    while (true){
        if (!getline(cin, input))
            return -1;

        ss.str(input);

        if (ss >> n)
            break;

        std::cout << "Invalid number, please try again" << std::endl;

        ss.clear();
        ss.str("");
        input.clear();
}

Why is it better than using cin >> n ?

Actual article explaining why

As for your question, use the above code to get the int value and then use it in the loop.

Cipher answered 25/11, 2015 at 15:34 Comment(0)
L
0

Don't use getline:

int totalquestions;
cin >> totalquestions;
Lyell answered 30/4, 2011 at 20:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.