how will this short c++ code look in pure c?

This is a discussion on how will this short c++ code look in pure c? within the C Programming forums, part of the General Programming Boards category; how will this short c++ code look in pure c? int main(void) { int i; do { cout << "[1] ...

  1. #1
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140

    how will this short c++ code look in pure c?

    how will this short c++ code look in pure c?

    int main(void)
    {
    int i;
    do
    {
    cout << "[1] Func1 << "\n";
    cout << "[0] End << "\n" << endl;

    cin >> i;

    switch(i)
    {
    case 1:
    Func1();
    break;
    case 0:
    break;
    default: cout << "Error" << endl;
    }
    }
    while (i != 0);
    return 0;
    }

  2. #2
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    Code:
    int main(void)
    {
        int i;
        do
        {
            printf( "[1] Func1\n" );
            printf( "[0] End\n\n" );
    
            i = getchar();
    
            switch(i)
            {
                case 1:
                    Func1();
                    break;
                case 0:
                    break;
                default: 
                    printf( "Error" );
                    break;
            }
        }
        while (i != 0);
        return 0;
    }
    pointer = NULL

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Close:
    > cout << "Error" << endl;
    > printf( "Error" );

    Forgot the newline.

    puts("Error");

    Quzah.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Don't know much of C++, but it looks like the i is being used as an int, so instead of
    Code:
    i = getchar ();
    you may want
    Code:
    scanf ("%d", &i);

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually, all of the *getc*() functions return integers.

    Quzah.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Yea, but if the user types in 1, then the value stored in i is not going to be 1.. it's gonna be '1'.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This is true. However, if their case statement was designed right it won't matter. But yeah, you're right there.

    Quzah.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    24
    Yes...your code will work just fine. but how much important the default statement is for our purpose is my question....
    RaHaTk

  9. #9
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    The default statement is simple error checking. If the user enters 3 for instance, what happens? The result is undefined, so it's a good idea to check and see if the value input was what you want, and if not it does nothing but restart the menu.
    pointer = NULL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling IRichEditOle interface methods
    By Niara in forum C Programming
    Replies: 2
    Last Post: 01-16-2009, 12:23 PM
  2. Compiler/Class bug?
    By Blackroot in forum C++ Programming
    Replies: 2
    Last Post: 01-31-2006, 10:56 PM
  3. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 09:17 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

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