Search code examples
c++syntaxidentifier

C++ | Syntax Error: Identifier 'i'


Hey guys, I'm pretty sure someone else has had this problem too, but I just couldn't find any related problems. This is also probably something really stupid like a typo or something, but I'm not able to figure it out >.<

What's wrong with the code, I always get this error:

error C2061: syntax error : identifier 'i'

#include <iostream>
#include <string>
using namespace std;

class MahinLuokka {
    public:
        void setNum(int);
        int getNum();
    private:
        int mahi_num;
};

int main()
{
    int i;
    do {
        cout << "Insert number between 1-100" << endl;
        cin >> i;
    } while i > 100 || i < 0;
    MahinLuokka mahi;
    mahi.setNum(i);
    cout << mahi.getNum() << endl;
    mahi.setNum(5);
    cout << "mahi_num set to 5" << endl;
    cout << mahi.getNum() << endl;

    // end
    int x;
    cin >> x;
    return 0;
}

void MahinLuokka::setNum(int number)
{
    mahi_num = number;
}

int MahinLuokka::getNum()
{
    return mahi_num;
}

Solution

  • You need to enclose the conditions in parentheses. In other words, change this:

    } while i > 100 || i < 0;
    

    To this:

    } while(i > 100 || i < 0);