Some help with my simple C++ code

Cheetoz

[H]ard|Gawd
Joined
Mar 3, 2003
Messages
1,972
the first part is I can't get an or condition to work. Suppose I have

Code:
bool done = false;
int value;

while(!done)
{
      cin >> value;
      if(value==1 || value==2)
             done=true;
}

and I enter 1, 2, or anything at all, the loop never ends. Yet if i do
Code:
 if(value>0 || value <3)
the code ends up working. Why is this so?

Secondly, I don't understand while making a struct compiles, but changing it to class does not.

This compiles:

Code:
#ifndef TEMPCOVERTER_H
#define TEMPCOVERTER_H

struct Temp {

    double value;


    Temp ();
    Temp (double temp);
    Temp convert2F();
    Temp convert2C();
    double print();
};

#endif // TEMPCOVERTER_H

and this

Code:
#ifndef TEMPCOVERTER_H
#define TEMPCOVERTER_H

class Temp {

    double value;

Public:
    Temp ();
    Temp (double temp);
    Temp convert2F();
    Temp convert2C();
    double print();
};

#endif // TEMPCOVERTER_H

gets the errors
Code:
tempCoverter.h:11: error: invalid use of undefined type 'class Temp'
tempCoverter.h:6: error: forward declaration of 'class Temp'
tempCoverter.h:11: error: ISO C++ forbids declaration of 'Public' with no type
tempCoverter.h:6: error: candidates are: Temp::Temp(const Temp&)
tempCoverter.h:12: error: Temp::Temp(double)

Thanks for any help.
 
"Public" should be all lowercase. The compiler error kinda hints at that... unclearly.
 
My hint for you on the first one would be to look at this table:
http://www.cpptutor.com/ascii.htm

The ASCII character '1' is equivalent to the integer 49, which is why your comparison works.... 49 is greater than 0.

For that second comparison, I'd recommend using &&, not || , otherwise every valid number will return true for either of those statements (and in some cases, for both of those statements.)

2.png
 
To add to Tytalus's answer the cin command does not take in the value as an integer, but as a character (ASCII) and assigns the int to the ascii value. So the first code you posted will work if (value == 49 || value == 50).

A simple way to debug your particular case for not understanding why the boolean statement is broken is to cut the value you are comparing.
 
To add to Tytalus's answer the cin command does not take in the value as an integer, but as a character (ASCII) and assigns the int to the ascii value. So the first code you posted will work if (value == 49 || value == 50).

A simple way to debug your particular case for not understanding why the boolean statement is broken is to cut the value you are comparing.

Well, I was kinda' hoping the OP would figure that out on their own time, but it's all good.
 
"Public" should be all lowercase. The compiler error kinda hints at that... unclearly.

ahhh, thanks.

To add to Tytalus's answer the cin command does not take in the value as an integer, but as a character (ASCII) and assigns the int to the ascii value. So the first code you posted will work if (value == 49 || value == 50).

I see. I googled and I found to use this
Code:
 if(std::cin.fail())
            {
                std::cin.clear();
                std::string clear;
                getline(std::cin, clear);
            }

so now I do
Code:
        bool done2 = false;
        int choice;
        while(!done2)
        {
            std::cin >> choice;
            if(std::cin.fail())
            {
                std::cin.clear();
                std::string clear;
                getline(std::cin, clear);
            }
            else
            {
                if(choice==1 || choice==2)
                    done2=true;

                if(choice==3)
                    return(0);
         }
            }
that should still get the value as ASCII, right? But it works now.
 
Yeah, I haven't used C++ in a few years, but I could have sworn that the std cin worked correctly for the type of variable you're inputting into. Yep, just checked my nonlinear data structs code, a "cin >> myInt;" sets the int value you're expecting correctly with no conversion necessary. Otherwise, it would be a major pain in the butt. I see no reason why the initial code wouldn't have worked.

Also on a sidenote, it's amazing how bad my OO programming was 10 years ago, LOL. 1 huge class with a huge method, and a couple little utility methods.
 
For user input (like typing), you can get the input line as a string. Then, you can just check for "1" or "2". Or, if you really want the input as an int, you can convert it (while validating it). I find that easier than message with cin >>, its buffer and cin.ignore etc.

Here's an example (where you want the conditionals to be ints)

Code:
#include <iostream>
#include <string>
#include <sstream>
#include <stdexcept>
using namespace std;

int int_from_string(const string& s) {
    const bool fail1 = s.find_first_not_of("-0123456789") != string::npos;
    const bool fail2 = s.size() > 1 && s[0] == '0';
    const bool fail3 = s.size() > 1 && s[0] == '-' && s[1] == '0';
    if (fail1 || fail2 || fail3) {
        throw runtime_error("String does not represent an integer");
    }
    istringstream ss(s);
    ss.exceptions(ios_base::badbit | ios_base::failbit);
    int num;
    ss >> num;
    return num;
}

int main() {
    while (true) {
        cout << "\n1. Item 1\n2. Item 2\n\nEnter choice: ";
        string line;
        getline(cin, line);
        int i;
        try {
            i = int_from_string(line);
        } catch (const exception&) {
            cerr << "\n" << line << " is not an integer\n";
            continue;
        }
        if (i == 1 || i == 2) {
            cout << "\nDone!\n";
            break;
        }
        cerr << "\n" << i << " is not an available option\n";
    }
}
 
Back
Top