1

I just started c++ like a week ago and not sure how to solve this error! I'm not sure if I have declared the job variable correctly...so if it is the mistake plz tell me how to declare it! If the input for cin>>job is none I want the if statement to be true and the output to be "Accepted!"

#include <iostream>

using namespace std;

int main()
{
    int age;

    char job;
    
    cout<<"Enter your age:";

    cin >> age;

    cout<<"\n";

    cout<<"Enter your job:";

    cin>>job;

    cout<<"\n";

    if (age > 18 && age < 60 && job == none ) {
        cout << "Accepted!" << endl;
    }

    return 0;
}

ERROR message:

./Playground/file0.cpp: In function 'int main()':
./Playground/file0.cpp:16:40: error: 'none' was not declared in this scope
   16 |     if (age > 18 && age < 60 && job == none ) {
      |                                        ^~~~
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • 2
    Yes, `none` is just a variable name that has not been declared anywhere. Can you explain what you want `job == none` to actually do? – cigien Aug 08 '20 at 14:23
  • 1
    Also, `job` is just one `char`, not large enough for a whole string. – 001 Aug 08 '20 at 14:26
  • How should I declare the word job if not through char data type? – Madhoora Mohan S Aug 08 '20 at 14:31
  • 3
    Use a `std::string`. You're still going to have issues reading an empty string, so look at `getline` as well. – cigien Aug 08 '20 at 14:31
  • 1
    may be you wanted `std::string job; ... if (age > 18 && age < 60 && job == "none" ) {` ? – bruno Aug 08 '20 at 14:35
  • If you have not learnt about `std::string` yet, then you will probably have to use a C-style string, which is a `char *`. If you have not learnt about them yet either, then you will probably have to learn a bit more before you can solve the task. – Andreas Wenzel Aug 08 '20 at 14:39
  • 4
    @AndreasWenzel Why would you teach someone C-style strings before C++ strings? It's not the C++ way to solve it and it's not simpler. – Thomas Sablik Aug 08 '20 at 14:49
  • @ThomasSablik: Maybe because the teacher wants to first teach the basics of how computers work and then later move on to more abstract programming? This topic has been the subject of controversial debate in the following question: [Should I learn C before learning C++?](https://stackoverflow.com/q/598552/12149471) – Andreas Wenzel Aug 08 '20 at 15:28
  • 1
    @AndreasWenzel OP tagged the question with C++. The question clearly states that OP wants to learn C++. The C++ way to work with strings is `std::string`. Why would you confuse OP and mention C-strings? Because _maybe_ some (usually old) teachers (usually without real world programming experience) teach it this way? – Thomas Sablik Aug 08 '20 at 15:45
  • @ThomasSablik I agree with you, this is why I insist with remarks on the two answers to 'force' their authors make them clear – bruno Aug 08 '20 at 15:48
  • @ThomasSablik: C-style strings belong just as much to the ISO C++ standard as `std::string`. We have no way of knowing what style of programming the OP is being taught. Therefore, I believe it was worth mentioning to the OP that if he has not yet learnt `std::string` but has learnt C-style strings, then he should use C-style strings. – Andreas Wenzel Aug 08 '20 at 15:54
  • Just as a side note: [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/12149471) – Andreas Wenzel Aug 08 '20 at 16:05

0 Answers0