What is the problem here? (noob question)

This is a discussion on What is the problem here? (noob question) within the C++ Programming forums, part of the General Programming Boards category; Right, so I am trying to make a similar program to the one given in the second tutorial on this ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Location
    London
    Posts
    5

    What is the problem here? (noob question)

    Right, so I am trying to make a similar program to the one given in the second tutorial on this site:

    Code:
    #include <iostream>	
    
    using namespace std;
    		
    int main()                            // Most important part of the program!
    {
      int age;                            // Need a variable...
      
      cout<<"Please input your age: ";    // Asks for age
      cin>> age;                          // The input is put in age
      cin.ignore();                       // Throw away enter
      if ( age < 100 ) {                  // If the age is less than 100
         cout<<"You are pretty young!\n"; // Just to show you it works...
      }
      else if ( age == 100 ) {            // I use else just to show an example 
         cout<<"You are old\n";           // Just to show you it works...
      }
      else {
        cout<<"You are really old\n";     // Executed if no other statement is
      }
      cin.get();
    }
    However, mine is about which maths set at school you are in (sad I know but I could think of nothing better). Anyway, something seems to be incorrect here:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
     int mathsset;
     
     cout<<"Welcome to maths set opinionator v.1.0.\n\n";
     cout<<"Enter the number of your maths set from one to seven and hit ENTER: ";
     cin>> mathsset;
     cin.ignore();
    
     if ( mathsset = 1,2 ) {
                  cout<<"Congratulations, you are a maths genius!\n";
                  }
                  
     else if ( mathsset = 3,4,5 ) {
                        cout<<"Not bad, but you could be better.\n";
                        
                        }
                        
     else if ( mathsset = 6,7 ) {
                        cout<<"Now that's just plain rubbish.\n";
                        
                        }
                        
     else if ( mathsset < 1 ) {
                        cout<<"This is not a valid maths set number.\n";
                        
                        }
                        
     else if ( mathsset > 7 )
                        cout<<"This is not a vaild maths set number.\n";
                        }
    
            cin.get()            
    
    }
    I want the program to say something positive, be it anything, when the person is in the top two sets, average when in the three middle ones, and bad when in the last two. If someone enters a higher or lower number then there are sets, I want it to say "This is not a valid maths set number".

    My C++ compiler, Dev C++, says this when I try to compile and run:

    "expected constructor, deconstructor or type conversion before " . " token"

    "expected "," or ";" before "." token"

    "expected declaration before "}" token"

    I don't really understand this expect the second one.

    I have analysed the cprogramming code shown at the start and looked for differences, but can't find any. I have also read the tutorial over and over again.

    Help anyone?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > mathsset = 1,2
    mathsset == 1 || mathsset == 2
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Location
    London
    Posts
    5
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
     int mathsset;
     
     cout<<"Welcome to maths set opinionator v.1.0.\n\n";
     cout<<"Enter the number of your maths set from one to seven and hit ENTER: ";
     cin>> mathsset;
     cin.ignore();
    
     if ( mathsset == 1 || mathsset == 2 ) {
                  cout<<"Congratulations, you are a maths genius!\n";
                  }
                  
     else if ( mathsset == 3 || mathsset ==4 || mathsset ==5 ) {
                        cout<<"Not bad, but you could be better.\n";
                        
                        }
                        
     else if ( mathsset ==6 || mathsset==7 ) {
                        cout<<"Now that's just plain rubbish.\n";
                        
                        }
                        
    else {
         
                        cout<<"This is not a valid maths set number.\n";
                        
                        }
    
            cin.get()            
    
    }
    I have changed it to this, still the same problem.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    The only error I get in the above code is the missing semicolon for the cin.get()

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Yeah, I can't see anything wrong with it, nor can I see any reason why it shouldn't work as you so desire.

  6. #6
    Registered User
    Join Date
    Jul 2006
    Location
    London
    Posts
    5
    Quote Originally Posted by Link_26
    The only error I get in the above code is the missing semicolon for the cin.get()
    Ahhh! What an idiot of me. Thanks to both of you for your help!

    One last question: Why is the || needed to separate each one? Would commas not have been a better idea for the person who developed cpp?

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    Not really. The comma operator is also known as sequence evaluation operator and it has a different use.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    Quote Originally Posted by p_m63
    Ahhh! What an idiot of me. Thanks to both of you for your help!

    One last question: Why is the || needed to separate each one? Would commas not have been a better idea for the person who developed cpp?
    what he said above me ^ and the || operator translates to "or".

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> Would commas not have been a better idea for the person who developed cpp?

    But how would you know whether you were using them for doing an AND logical statement, or an OR statement, or a NOR, or an XOR etc. Unfortunately, I think the longer way is the only way to do it.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    if(x == 1,2,3)

    Hah. If only compilers were smart enough to do that!
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question About My Homework Pls Help Me İmmediately
    By jennyyyy in forum C Programming
    Replies: 27
    Last Post: 03-13-2008, 11:40 AM
  2. Real Noob question
    By Dark Greek in forum C++ Programming
    Replies: 8
    Last Post: 09-09-2007, 06:49 PM
  3. super noob question
    By verbity in forum C++ Programming
    Replies: 6
    Last Post: 06-23-2007, 11:38 AM
  4. Noob question (redeclaring a array)
    By Demon.killer in forum C Programming
    Replies: 8
    Last Post: 10-21-2006, 12:06 PM
  5. Noob question, command prompt
    By Gorillasnot in forum C Programming
    Replies: 8
    Last Post: 02-28-2006, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21