Results 1 to 15 of 15

Thread: Why wont this compile? (C++)

  1. #1
    Flak Monkey! Dorza's Avatar
    Join Date
    Jul 2003
    Location
    UK - South Wales
    Posts
    1,762
    Thanks
    34
    Thanked
    17 times in 15 posts
    • Dorza's system
      • Motherboard:
      • Asus P5B Deluxe - WiFi
      • CPU:
      • Q6600 @ 3.06Ghz
      • Memory:
      • 2GB Crucial
      • Storage:
      • 500GB Samsung SpinPoint
      • Graphics card(s):
      • Geforce 9600GT
      • PSU:
      • Cosair HX520W
      • Case:
      • LianLi something something or other
      • Monitor(s):
      • Eizo FlexScan S1910 (1280*1024)
      • Internet:
      • 2mb Virgin (when they want to give me that: else 1mb)

    Why wont this compile? (C++)

    After following a book for some time ive started to use my head and do a few simple programs of my own, the one im have a problem with is listed below.

    Code:
    #include<iostream>
    
    int main()
    {
        using std::cout;
        using std::cin;
        
        char day;
        cout << "What day is it: ";
        cin >> day;
        
        char reply;
        cin >> reply;
          
            if day = Saturday or Sunday;
            cout << "Its the weekend right?";
                {
                    if reply = yes;
                        cout <<"SYS msg" ;
                        char StopCharacter;
                            cout << "Press a key and \"Enter\" to end: " ;
                        cin >> StopCharacter;
                    else 
                    {
                        cout << "dont lie :-p. Its, " << day << "\n";
                    }
                }
        else 
            cout << "SYS msg" "\n";
        char StopCharacter;
        cout << "Press a key and \"Enter\" to end: " ;
         cin >> StopCharacter;
                
        return 0;
    }
    Basicly i want a user to enter a day, then the system says weather its the weekend or not. It it thinks its the weekend it will check this with the user , depending on what answer the user gives (yes/no) another msg will be displayed. If the user did not enter a weekend day the system will simply say its not the weekend for example.

    Im have a problem compileing the program tho.

    the compiler comes back with the following errors:

    Error E2376 ddd.cpp 17: If statement missing ( in function main()
    Error E2376 ddd.cpp 20: If statement missing ( in function main()
    Error E2054 ddd.cpp 25: Misplaced else in function main()
    Error E2054 ddd.cpp 31: Misplaced else in function main()
    Error E2134 ddd.cpp 38: Compound statement missing } in function main()

    Been trying to sort this out for a while now but i can't, any help would be appreciated thanks

    EDIT: im assuming that everytying else is ok cos i havent had a compile error else where yet. If theres other errors plz let me know
    Last edited by Dorza; 01-03-2004 at 12:38 AM.

  2. #2
    Senior Member Shad's Avatar
    Join Date
    Jul 2003
    Location
    In front
    Posts
    2,773
    Thanks
    22
    Thanked
    40 times in 24 posts
    Well I don't do C++ but I'm starting C. Shouldn't your if statements be similar to...

    Code:
    if (day == "Saturday" || day == "Sunday") {
    
    } else {
    
    }
    I dunno, maybe they changed it for C++ modules..?
    Simon


  3. #3
    Goat Boy
    Join Date
    Jul 2003
    Location
    Alexandra Park, London
    Posts
    2,428
    Thanks
    0
    Thanked
    0 times in 0 posts
    C and C++ are syntactically identical. You can compile a C program with a C++ compile quite easily. The first C++ compilers were simply parsers that bolted on top of C compilers and converted the code into C before running it through a standard C compiler.

    Anyway, yeah, your syntax is wrong sort of, um, everywhere

    I'd suggest trying to compile a one line program first, then a two line program etc. etc.

    As Shad said, your if statements need brackets around them, and they need to use the "is equal to" operator (==) as opposed to the assignment operator (=).

    char can only be 1 byte in length (i.e. 1 character). If you want to use strings you will need to use the String library (more info here: http://cplus.about.com/library/weekly/aa051202b.htm)

    Try and just get the progam compiling with a "Hello World" type app before doing anything else like if...then conditionals.


    For example, this compiles on my gentoo box:


    int main() {
    cout << "What day is it:\n ";
    return 0;
    }

    Using the compiler:

    g++ test.C -o hello
    "All our beliefs are being challenged now, and rightfully so, they're stupid." - Bill Hicks

  4. #4
    Member
    Join Date
    Sep 2003
    Location
    Nottingham, UK
    Posts
    152
    Thanks
    0
    Thanked
    0 times in 0 posts
    Don't mean to rain your parade, But I think it might be best to learn C first, I'm currently enjoying learning the beauty of C programming, And then learn C++ later when you understand the fundementals of C.
    AMD Athlon XP 2400+ | Connect3D Radeon 9600PRO | Seagate Barrcuda 80GB HDD | 512mb PC2700 Crucial RAM | MSI KT6 Delta FIS2R | Zorro Silver Case | Windows XP/Gentoo Linux

  5. #5
    dgr
    dgr is offline
    Senior Member
    Join Date
    Jul 2003
    Posts
    621
    Thanks
    0
    Thanked
    0 times in 0 posts
    well actually i would recommend learning java first then c++. if you don't then you won't understand OOP when moving to c++ - if you go c > c++, you will pick up bad habbits.

    dgr
    dothan 745 @ 2.4ghz | 2gb Corsair XMS (2-3-3-6) | dual raptors (raid0) | ATI 9700pro | CM201 | dual lg 1810

  6. #6
    only the finest beef
    Join Date
    Nov 2003
    Posts
    1,175
    Thanks
    4
    Thanked
    0 times in 0 posts
    try deleting the semi colons at the end of your lines beginning with "if"

    that might get rid of your first couple of errors

  7. #7
    HEXUS webmaster Steve's Avatar
    Join Date
    Nov 2003
    Posts
    14,276
    Thanks
    292
    Thanked
    837 times in 473 posts
    Quote Originally Posted by Angus
    try deleting the semi colons at the end of your lines beginning with "if"

    that might get rid of your first couple of errors
    I was going to suggest this, but I've only done PHP and Pascal.

    It should also get rid of your second two errors. The final else statement looks like it needs {} around the code you want to perform under that condition.

    Hope this helps.
    PHP Code:
    $s = new signature();
    $s->sarcasm()->intellect()->font('Courier New')->display(); 

  8. #8
    Sublime HEXUS.net
    Join Date
    Jul 2003
    Location
    The Void.. Floating
    Posts
    11,819
    Thanks
    213
    Thanked
    233 times in 160 posts
    • Stoo's system
      • Motherboard:
      • Mac Pro
      • CPU:
      • 2*Xeon 5450 @ 2.8GHz, 12MB Cache
      • Memory:
      • 32GB 1600MHz FBDIMM
      • Storage:
      • ~ 2.5TB + 4TB external array
      • Graphics card(s):
      • ATI Radeon HD 4870
      • Case:
      • Mac Pro
      • Operating System:
      • OS X 10.7
      • Monitor(s):
      • 24" Samsung 244T Black
      • Internet:
      • Zen Max Pro
    I think this:
    Code:
    #include<iostream>
    
    int main()
    {
        using std::cout;
        using std::cin;
        
        char day;
        cout << "What day is it: ";
        cin >> day;
        
        char reply;
        char reply2;
        cin >> reply;
          
            if (day == 'Saturday' || 'Sunday')
    	{
            	cout << "Its the weekend right?";
    		cin >> reply2;
                   	if (reply2 == 'yes')
    		{
                    	cout <<"SYS msg" ;
                    	char StopCharacter;
                         	cout << "Press a key and \"Enter\" to end: " ;
                    	cin >> StopCharacter;
    		}
                    else 
                    {
                    	cout << "dont lie :-p. Its, " << day << "\n";
                    }
            }
        	else 
            {
    		cout << "SYS msg" "\n";
    		char StopCharacter;
    		cout << "Press a key and \"Enter\" to end: " ;
    		cin >> StopCharacter;
            }
        
    return 0;
    }
    might be a little closer to the mark (but bare in mind I've not done any programming in aaaages, and it's 2am )
    (\__/)
    (='.'=)
    (")_(")

  9. #9
    Flak Monkey! Dorza's Avatar
    Join Date
    Jul 2003
    Location
    UK - South Wales
    Posts
    1,762
    Thanks
    34
    Thanked
    17 times in 15 posts
    • Dorza's system
      • Motherboard:
      • Asus P5B Deluxe - WiFi
      • CPU:
      • Q6600 @ 3.06Ghz
      • Memory:
      • 2GB Crucial
      • Storage:
      • 500GB Samsung SpinPoint
      • Graphics card(s):
      • Geforce 9600GT
      • PSU:
      • Cosair HX520W
      • Case:
      • LianLi something something or other
      • Monitor(s):
      • Eizo FlexScan S1910 (1280*1024)
      • Internet:
      • 2mb Virgin (when they want to give me that: else 1mb)
    Thanks for ALL ur replies so far. Sorry i havent replied. Been busy will college work, ne way Stoo, thats pretty good codein for 2am only error i get now is with the If statement:

    if (day == 'Saturday' || 'Sunday')

    the error

    Error E2129 string.cpp 16: Character constant must be one or two characters long in function main()

    I get that error twice and both times it points to the if statement. Ive tried various things but i just make it worse. Any ideas?

  10. #10
    Member
    Join Date
    Aug 2003
    Location
    Wimbledon
    Posts
    141
    Thanks
    0
    Thanked
    0 times in 0 posts
    You need to do something like this:

    Code:
    /* strcmp example */
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
      char szKey[] = "apple";
      char szInput[80];
      do {
         printf ("Which is my favourite fruit? ");
         gets (szInput);
      } while (strcmp (szKey,szInput) != 0);
      printf ("Correct answer!\n");
      return 0;
    }

  11. #11
    Member
    Join Date
    Sep 2003
    Location
    Nottingham, UK
    Posts
    152
    Thanks
    0
    Thanked
    0 times in 0 posts
    Mart, I think you will find he's attempting to learn C++, not C

    AMD Athlon XP 2400+ | Connect3D Radeon 9600PRO | Seagate Barrcuda 80GB HDD | 512mb PC2700 Crucial RAM | MSI KT6 Delta FIS2R | Zorro Silver Case | Windows XP/Gentoo Linux

  12. #12
    Goat Boy
    Join Date
    Jul 2003
    Location
    Alexandra Park, London
    Posts
    2,428
    Thanks
    0
    Thanked
    0 times in 0 posts
    Whether it is C or C++, the current exercise does not really make any difference as to what language you write it in. It will look the same.
    "All our beliefs are being challenged now, and rightfully so, they're stupid." - Bill Hicks

  13. #13
    Member
    Join Date
    Aug 2003
    Location
    Wimbledon
    Posts
    141
    Thanks
    0
    Thanked
    0 times in 0 posts
    I wanted to demonstrate the use of strcmp and arrays as they were the some of the main issues with the original code.

  14. #14
    Member
    Join Date
    Sep 2003
    Posts
    53
    Thanks
    1
    Thanked
    0 times in 0 posts
    Like everyone else I haven't touched c++ code for years but try:

    if ( (day == "Saturday") || (day == "Sunday") )

    You probably don't need the extra brackets but it adds clarity I think.
    Wow I just noticed that this is a couple of weeks too late, I guess you got it sorted by now anyway.

  15. #15
    Member
    Join Date
    Aug 2003
    Location
    Wimbledon
    Posts
    141
    Thanks
    0
    Thanked
    0 times in 0 posts
    Quote Originally Posted by idlewild
    Like everyone else I haven't touched c++ code for years but try:

    if ( (day == "Saturday") || (day == "Sunday") )

    You probably don't need the extra brackets but it adds clarity I think.
    Wow I just noticed that this is a couple of weeks too late, I guess you got it sorted by now anyway.
    That wont work, you need to use strcmp()...

    Code:
    if(strcmp(day, "Saturday") == 0 || strcmp(day, "Sunday") == 0 )
    {
        // Its true
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •