Do while loop repeats infinitely

Pages: 12
I am afraid that's not really a solution. You can have a full explanation if you really want one, but for now just remember to not do that.
I ran hanst99's code and it works propertly. I also use Xp, so I don't think thats the problem. Maybe the software you use has to be updated or changed or something.
I was thinking of releasing this to a few people, so I was trying to add error checking, could you explain it ti me completely?, this is my first program after all.
Could you please avoid releasing your early programs to the public? I am not saying this is your fault or implying anything regarding your ability, but if you just started out on programming you WILL screw up and you WILL write messy code, that would at best be useless, at worst lead to a spread of bad practices.
Okay then, I just said screw it and decided to stop trying to check for errors, I'll probably be the only one to use it anyway.
I don't do a lot with iostream anymore, but I have an idea.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace std;

int main(){
    string buf;
    bool valid = false;
    int year = 0;

    while (year==0){
        getline(cin, buf); //get string, can contain any character w/o error
        for (int s = 0; s<buf.size(); s++){ //i'm not sure if size() exists for strings however
        //check that buf has no letters
        if (buf(has no letters)){ //this isn't valid, just here for example
        valid = true;}

    if (valid)
    year >> buf;}

//the rest of your program... 


Just a thought
Why do such a random thing? You can check the state of streams... cin.fail() returns true if the stream is, for some reason, broken. If it failed you can either let the program crash with an error, or clear the error flags with cin.clear, cin.ignore cin.rdbuf()->in_avail() to remove all characters from the buffer, and then just try again.
Topic archived. No new replies allowed.
Pages: 12