C++ Quiz Problem

This is a discussion on C++ Quiz Problem within the C++ Programming forums, part of the General Programming Boards category; Hi Im new to this forum, I just signed up I have a problem with my code. I am trying ...

  1. #1
    EMH
    EMH is offline
    Registered User
    Join Date
    Jul 2009
    Posts
    7

    C++ Quiz Problem

    Hi

    Im new to this forum, I just signed up

    I have a problem with my code. I am trying to make a quiz, it was working fine yesterday. I tried to debug it today and i got 29 errors!

    I dont understand how that happened, I have looked at the output and I just dont see whats wrong with it. Any help would be greatly appreciated, and yes I a noob

    Code is below:

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <iostream>
    
    int main();
    }
    char a;
    printf("GENERAL QUIZ\n");
    printf("\n");
    printf("Who is the chairman of Microsoft Corp?\n");
    printf("1 Bill Gates\n");
    printf("2 Steve Jobs\n");
    printf("3 Jake Taylor\n");
    a=getchar();
    if (a=='1') printf("Correct\n"); else printf("Wrong\n");
    while (a!='z');
      {
      a=getchar();
    {
    EDIT: You'll have to explain things carefully as I can get easily confused
    Last edited by EMH; 07-03-2009 at 03:55 AM.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    There is an excess semicolon after main(), and another after while(). Your code block brackets don't match. You always begin a block with {, and end with }. If you start, you must end it, therefore there will always be an even number of brackets.
    1st bracket: backwards
    2nd: ok
    3rd: backwards
    4th: awol

    Also, you are missing a return 0; statement at the end of main().

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    40
    You should learn how to indent your code properly as well for readability purposes, especially when you're debugging.

  4. #4
    EMH
    EMH is offline
    Registered User
    Join Date
    Jul 2009
    Posts
    7
    Ok thanks. I'll try what you (rossipoo) suggested later. I'll let you know how I get on. Thanks again

  5. #5
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    For future reference, it helps if you post the errors that you receive.
    MSDN <- Programmers Haven!

  6. #6
    EMH
    EMH is offline
    Registered User
    Join Date
    Jul 2009
    Posts
    7
    Ok, Im on one error now. I still dont understand it though.

    : fatal error C1075: end of file found before the left brace '{' at 'c:\users\------ ----------documents\visual studio 2008\projects\test1.1\test1.1\test1.1.cpp(6)' was matched

  7. #7
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Post your updated code.
    MSDN <- Programmers Haven!

  8. #8
    EMH
    EMH is offline
    Registered User
    Join Date
    Jul 2009
    Posts
    7
    Sorry. I have to get used to this

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <iostream>
    
    int main()
    {
    char a;
    printf("GENERAL QUIZ\n");
    printf("\n");
    printf("Who is the chairman of Microsoft Corp?\n");
    printf("1 Bill Gates\n");
    printf("2 Steve Jobs\n");
    printf("3 Jake Taylor\n");
    a=getchar();
    if (a=='1') printf("Correct\n"); else printf("Wrong\n");
    while (a!='z')
      {
      a=getchar();
    }

  9. #9
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    You get the error because your braces don't match, like pointed out earlier.

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <iostream>
    
    int main()
    {
    char a;
    printf("GENERAL QUIZ\n");
    printf("\n");
    printf("Who is the chairman of Microsoft Corp?\n");
    printf("1 Bill Gates\n");
    printf("2 Steve Jobs\n");
    printf("3 Jake Taylor\n");
    a=getchar();
    if (a=='1') printf("Correct\n"); else printf("Wrong\n");
    while (a!='z')
      {
      a=getchar();
      }
    }
    Also, look into indentation.
    MSDN <- Programmers Haven!

  10. #10
    EMH
    EMH is offline
    Registered User
    Join Date
    Jul 2009
    Posts
    7
    WOW. It works and I feel so stupid, haha

    Thanks guys, it really means alot. I hope I'll stay here and learn alot from you guys.
    Thanks again

  11. #11
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    No problemo!

    By the way, do you realise that your print functions are in C, not C++?
    MSDN <- Programmers Haven!

  12. #12
    EMH
    EMH is offline
    Registered User
    Join Date
    Jul 2009
    Posts
    7
    Actaully no I didn't.

    My teacher showed me then, he mainly used C and not C++.

    How do I change these to C++ Print Functions, and whats the difference between the C version and the C++ version?

    Thanks

  13. #13
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Well I'm not a C programmer, so I can't tell you the difference between the two, but I can tell you how to translate between the two.

    To print text to the console, use std::cout followed by the output operator <<

    To acquire user input and store it into a variable, use std::cin followed by the input operator >>

    I'll demonstrate:

    Code:
    #include <iostream>
    
    int main()
    {
        char a;
        std::cout << "GENERAL QUIZ\n";
        std::cout << "\n";
        std::cout << "Who is the chairman of Microsoft Corp?\n";
        std::cout << "1 Bill Gates\n";
        std::cout << "2 Steve Jobs\n";
        std::cout << "3 Jake Taylor\n";
        std::cin >> a;
        if (a == '1')
        {
            std::cout << "Correct\n";
        }
        else
        {
            std::cout << "Wrong\n";
        }
        while (a != 'z')
        {
            std::cin >> a;
        }
    }
    As you can see, I've removed outdated header files that aren't needed for the program, and replaced your C functions with C++ functions. The braces highlighted are my programming style although, feel free to use your own. :P
    Last edited by legit; 07-03-2009 at 10:13 AM.
    MSDN <- Programmers Haven!

  14. #14
    EMH
    EMH is offline
    Registered User
    Join Date
    Jul 2009
    Posts
    7
    Ah i see the difference.

    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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