Jump to content

Looks silly&driving me nuts

- - - - -

  • Please log in to reply
13 replies to this topic

#1
Bat0u89

Bat0u89

    Newbie

  • Members
  • PipPip
  • 25 posts
#include "stdafx.h"
#include <string>
#include <sstream>
#include <iostream>
using namespace std;

int getInt()
{ 
    int y;
    string line;
    stringstream input;
    while(true){
        getline(cin,line);
        input.write(line.c_str(),10);
        if(input>>y)
            return y;
        cout<<"Error, try again."<<endl;
        input.seekp(ios_base::beg);
        input.seekg(ios_base::beg);
    }
}
As you can see I'm trying to read an int from cin...
Whenever I input a wrong value like "asdfasd" it won't accept any correct value after that.

Edited by Alexander, 01 February 2011 - 05:28 PM.
Corrected <code> in to [code]


#2
Xupicor

Xupicor

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
Short answer - you don't clear your buffer after wrong value was given.
See here:
#include <iostream>

#include <string>

#include <sstream>


template <typename T>

T cin_get(const std::string& msg = ">> ", const std::string& error_msg = "Provide correct value, please.") {

    T buffer;

    for (;;) {

        std::cout << msg;

        if (std::cin >> buffer && std::cin.peek() == '\n') {

            break;

        } else {

            std::cout << error_msg << '\n';

            std::cin.clear();

            std::cin.sync();

        }

    }

    return buffer;

}


int main() {

    int a = cin_get<int>("Gimme an int! >>", "Naw bro, an int!");

    float b = cin_get<float>("Floats only!: ");

    std::string s = cin_get<std::string>("What's your name?\n>> ");

    

    return 0;

}



#3
Bat0u89

Bat0u89

    Newbie

  • Members
  • PipPip
  • 25 posts
Could you please explain why are you doing things like clearing the error bits of cin? and sync discards any unread characters but I still don't understand why that's important :\ .I also tried cin.clear();cin.sync(); at the end of my loop but it didn't solve my problem...

#4
Bat0u89

Bat0u89

    Newbie

  • Members
  • PipPip
  • 25 posts
ok i also tried input.clear();input.sync(); :P

#5
Bat0u89

Bat0u89

    Newbie

  • Members
  • PipPip
  • 25 posts
btw that didn't solve my problem

#6
Xupicor

Xupicor

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
Should've said so, I thought it did. ;) Here:
int getInt() {

    int y;

    string line;

    stringstream input;

    while (true) {

        getline(cin, line);

        input << line;

        if (input >> y)

            return y;

        cout << "Error, try again.\n";

        input.clear(); // clear error flags

        input.str(""); // clear the buffer.

    }

}


#7
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
You know what always works for me? The ignore() child. Just try input.ignore() instead of input.clear() and input.str("").
Latinamne loqueris?

#8
Xupicor

Xupicor

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
I already provided working solution. ;)
See ignore - C++ Reference , ignore() member function (what do you mean by child?) does a bit different job, and it doesn't clear error flags.

#9
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
I'm just saying that's what works for me. Whenever I have a loop and am using cin, I just use cin.ignore() at the end of the loop, and it works like a charm. What's so important about clearing the error flags, anyway?
Latinamne loqueris?

#10
Xupicor

Xupicor

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
Are you trying to tell me you just use it "because it works" without trying to understand what it actually does (or just reading docs)? That's a bad idea IMO.

Just copy paste the code and see for yourself. If you won't clear the error flags of the stringstream object (which is used in a loop) then you will never get out of the loop after providing bad input, because "if (input >> y)" will never yield true.

#11
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
All I know it does is clears the buffer. That's what I use it for. I don't generally use if statements like that, by putting something like "input >> y" as the conditional statement.
Latinamne loqueris?

#12
Xupicor

Xupicor

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
I'm sorry, but I won't be able to help you understand why certain things work or not without you trying to do so too. All I could do now is repeat what I wrote last time. ;)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users