CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 42
  1. #1
    Join Date
    Feb 2012
    Posts
    68

    C++/C conversion to just C

    Below is a program that generates random Addition for Subtraction problems depending on the user's choice. The user is prompted to input an answer and then it keeps your score. If you want to quit you just press zero. This is pretty much my first C program ever, I read the first eight chaps of my C/c++ book and along with supplementary online readings, made it. The thing is, it is suppose to be pure C! I was looking for advice on how to salvage this, please give me your wisdom





    Code:
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    
    
    int menu();
    bool addition(int &reward); // function to return truth values
    bool subtraction(int &reward);
    
    int main()
    {
    
    
    
    bool Result, Finished = false;
    int choice, reward, score = 0;
    
    while(!Finished) // while finished is not true
    {
        cout << "\nscore:" << score; // print the score
    
    
    
    {
    choice = menu(); // calls for user input and enters it to switch
    
    
    
    
    
        switch  (choice){ // accesses the functions
                case 0: Finished = true;                     break;
                case 1: Result = addition(reward);           break;
                case 2: Result = subtraction(reward);        break;
                default : cout << "Error in input\n\n";      break;
        }
        if(Result == true){
            score += reward; // score = score + reward
            }
    
    }
    }
    }
    int menu() //main menu that gets called and allows for user input
    {
        int MenuChoice;
            cout << "\n";
            cout << "Enter 1 for addition\n";
            cout << "Enter 2 for Subtraction\n";
            cout << "Enter 0 to quit\n";
            cout << "-----------------\n";
            cout << "Choice: ";
        cin >> MenuChoice; // user input
        return MenuChoice; //return the choice to choice to select a case
    }
    
    
    
    bool addition(int &reward) //function that is called by the users selection
    {
    
    int answer;
    int random1, random2;
    reward = 1;
    
    
    srand(time(NULL)); //generates fresh numbers each time.
    
        random1 = 1+(rand()&#37;99); // from 1-99
        random2 = 1+(rand()%99); // from 1-99
    
    
        cout << "\n" << random1 << " + " << random2 << " = "; //code to format the random math problem
        cin >> answer; // user input of answer
    {
    
    
    
    
    if (answer == (random1+random2)){
            cout << "Congratulations\n\n";
                return true;// tells result whether to add a pt or not
            }   //if user is correct than the score is incremented
        else{
            cout << "Incorrect\n\n";} //if not than the score is not incremented
    
    return false;
    
    }
    }
    
    
    
    bool subtraction(int &reward) // follows same model as previous function, but instead subtraction.
    {
    
    
        int answer;
        int random1, random2;
        reward = 1;
    
    
    
    srand(time(NULL));
    
        random1 = 1+(rand()%99); // from 1-99
        random2 = 1+(rand()%99); // from 1-99
    
    
    
        cout << "\n" << random1 << " - " << random2 << " = ";
        cin >> answer;
    
    
        if (answer == (random1-random2)){
            cout << "Congratulations\n\n";
                return true; // tells result whether to add a pt or not
            }
            else{
                cout << "Incorrect\n\n";}
    
    return false;
    
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: C++/C conversion to just C

    For starters, indent your code properly. Next, it seems that the main C++ thing that you are using here is C++ I/O streams, so you just need to change them to use C I/O, e.g.,
    Code:
    cout << "\n" << random1 << " - " << random2 << " = ";
    would become:
    Code:
    printf("\n&#37;d - %d = ", random1, random2);
    Of course, you should then remove the inclusions of <iostream> and <iomanip>. It looks like you are not using std::string, so you can just remove the inclusion for <string>.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: C++/C conversion to just C

    If it were me, I'd compile it as a C program and see what the compiler complained about, then make appropriate changes.

  4. #4
    Join Date
    Feb 2012
    Posts
    68

    Re: C++/C conversion to just C

    any ideas on how to replace the bool with a C equivalent?

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: C++/C conversion to just C

    The normal C-way is just an int. A non-zero value is true and a zero value is false.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  6. #6
    Join Date
    Feb 2012
    Posts
    68

    Re: C++/C conversion to just C

    Okay so I swapped out the bools and couts and cin with ints, printfs and scanf. It wont compile saying that their is something wrong with the &reward int the functions

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    
    
    
    
    int menu();
    int addition(int &reward); // function to return truth values
    int subtraction(int &reward);
    
    int main()
    {
    
    
    
    int Result, Finished = 0;
    int choice, reward, score = 0;
    
    while(Finished != 1); // while finished is not true
    {
        printf("\nscore: &#37;d", score); // print the score
    
    
    
    {
    choice = menu(); // calls for user input and enters it to switch
    
    
    
    
    
        switch  (choice){ // accesses the functions
                case 0: Finished = 1;                     break;
                case 1: Result = addition(reward);           break;
                case 2: Result = subtraction(reward);        break;
                default : printf("Error in input\n\n");      break;
        }
        if(Result == 1){
            score += reward; // score = score + reward
            }
    
    }
    }
    }
    int menu() //main menu that gets called and allows fr user input
    {
        int MenuChoice;
            printf("\n");
            printf("Enter 1 for addition\n");
            printf("Enter 2 for Subtraction\n");
            printf("Enter 0 to quit\n");
            printf("-----------------\n");
            printf("Choice: ");
        scanf("%d", &MenuChoice); // user input
        return MenuChoice; //return the choice to choice to select a case
    }
    
    
    
    int addition(int &reward) //function that is called by the users selection
    {
    
    int answer;
    int random1, random2;
    reward = 1;
    
    
    srand(time(NULL)); //generates fresh numbers each time.
    
        random1 = 1+(rand()%99); // from 1-99
        random2 = 1+(rand()%99); // from 1-99
    
    
        printf("\n%d - %d = ", random1, random2); //code to format the random math problem
        scanf("%d", &answer); // user input of answer
    {
    
    
    
    
    if (answer == (random1+random2)){
            printf("Congratulations\n\n");
                return 1;// tells result whether to add a pt or not
            }   //if user is correct than the score is incremented
        else{
            printf("Incorrect\n\n");} //if not than the score is not incremented
    
    return 0;
    
    }
    }
    
    
    
    int subtraction(int &reward) // follows same model as previous function, but instead subtraction.
    {
    
    
        int answer;
        int random1, random2;
        reward = 1;
    
    
    
    srand(time(NULL));
    
        random1 = 1+(rand()%99); // from 1-99
        random2 = 1+(rand()%99); // from 1-99
    
    
    
        printf("\n%d - %d = ", random1, random2);
        scanf("%d", &answer);
    
    
        if (answer == (random1-random2)){
            printf("Congratulations\n\n");
                return 1; // tells result whether to add a pt or not
            }
            else{
                printf("Incorrect\n\n");}
    
    return 0;
    
    }

  7. #7
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: C++/C conversion to just C

    C doesn't support C++ references so they have to be replaced with pointers instead.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++/C conversion to just C

    Quote Originally Posted by chucker View Post
    Below is a program that generates random Addition for Subtraction problems depending on the user's choice. The user is prompted to input an answer and then it keeps your score. If you want to quit you just press zero. This is pretty much my first C program ever, I read the first eight chaps of my C/c++ book
    What book teaches C/C++?

    C and C++ are two separate languages -- either the book teaches C++, or the book teaches C. I know of no book that teaches both languages, and it would be foolhardy (IMO) to have such a book due to the differences in the languages, what is considered legal/illegal syntax, programming paradigms, etc. Such a book, if it exists, would only confuse the programmer trying to learn one language over the other.

    Second, since the assignment sounds straightforward, pretend your C++ program doesn't exist. Write it from scratch using 'C'.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Jan 2009
    Posts
    596

    Re: C++/C conversion to just C

    Quote Originally Posted by chucker View Post
    any ideas on how to replace the bool with a C equivalent?
    Quote Originally Posted by S_M_A View Post
    The normal C-way is just an int. A non-zero value is true and a zero value is false.
    C99 introduced 'bool' as a type, along with 'true' and 'false', of course. Although they are just macros under the hood, using them makes the program semantics clearer.
    Just make sure to #include <stdbool.h> to use them.
    Last edited by Peter_B; February 18th, 2012 at 05:44 PM.

  10. #10
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: C++/C conversion to just C

    There has been a long debate about the macro version of bool in C being good or not and I'm in the not good club.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  11. #11
    Join Date
    Feb 2012
    Posts
    68

    Re: C++/C conversion to just C

    Quote Originally Posted by Paul McKenzie View Post
    What book teaches C/C++?

    C and C++ are two separate languages -- either the book teaches C++, or the book teaches C. I know of no book that teaches both languages, and it would be foolhardy (IMO) to have such a book due to the differences in the languages, what is considered legal/illegal syntax, programming paradigms, etc. Such a book, if it exists, would only confuse the programmer trying to learn one language over the other.
    Book is C how to program

    it does C then C++ later.

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++/C conversion to just C

    Quote Originally Posted by chucker View Post
    Book is C how to program

    it does C then C++ later.
    That is no different than a book that teaches both FORTRAN and Pascal, assuming that FORTRAN is a prerequisite to learning Pascal. It makes no sense IMO.

    You're either learning C or learning C++. You're either thinking in 'C' or thinking in C++.

    See this link about learning 'C' as a prerequisite to learning C++ (which I would think this book is attempting to do):

    http://www.parashift.com/c++-faq-lit...n-c-first.html

    So again, pretend your program you've written in C++ does not exist at all, and you're a brand new programmer to the 'C' world.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 19th, 2012 at 03:42 AM.

  13. #13
    Join Date
    Feb 2012
    Posts
    68

    Re: C++/C conversion to just C

    Quote Originally Posted by S_M_A View Post
    C doesn't support C++ references so they have to be replaced with pointers instead.
    Any advice on how to implement the pointers as a replacement? I know the basics of them, but not quite how to apply them to my existing program. I had an attempt below to use pointers but failed hard.



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    
    
    
    
    int menu();
    void addition(int *reward); // function to return truth values
    void subtraction(int *reward);
    
    int main()
    {
    
    
    
    int Result, Finished = 0;
    int choice, score = 0;
    int rewards = 0;
    
    while(Finished != 1); // while finished is not true
    {
        printf("\nscore: &#37;d", score); // print the score
    
    
    
    {
    choice = menu(); // calls for user input and enters it to switch
    
    
    
    
    
        switch  (choice){ // accesses the functions
                case 0: Finished = 1;                     break;
                case 1: addition(&rewards);           break;
                case 2: subtraction(&rewards);        break;
                default : printf("Error in input\n\n");      break;
        }
        if(rewards == 1){
            score += rewards; // score = score + reward
            }
    
    }
    }
    }
    int menu() //main menu that gets called and allows fr user input
    {
        int MenuChoice;
            printf("\n");
            printf("Enter 1 for addition\n");
            printf("Enter 2 for Subtraction\n");
            printf("Enter 0 to quit\n");
            printf("-----------------\n");
            printf("Choice: ");
        scanf("%d", &MenuChoice); // user input
        return MenuChoice; //return the choice to choice to select a case
    }
    
    
    
    void addition(int *reward) //function that is called by the users selection
    {
    
    int answer;
    int random1, random2;
    
    
    
    srand(time(NULL)); //generates fresh numbers each time.
    
        random1 = 1+(rand()%99); // from 1-99
        random2 = 1+(rand()%99); // from 1-99
    
    
        printf("\n%d - %d = ", random1, random2); //code to format the random math problem
        scanf("%d", &answer); // user input of answer
    {
    
    
    
    
    if (answer == (random1+random2)){
            printf("Congratulations\n\n");
                *reward++;// tells result whether to add a pt or not
            }   //if user is correct than the score is incremented
        else{
            printf("Incorrect\n\n");} //if not than the score is not incremented
    
    
    
    }
    }
    
    
    
    void subtraction(int *reward) // follows same model as previous function, but instead subtraction.
    {
    
    
        int answer;
        int random1, random2;
    
    
    
    
    srand(time(NULL));
    
        random1 = 1+(rand()%99); // from 1-99
        random2 = 1+(rand()%99); // from 1-99
    
    
    
        printf("\n%d - %d = ", random1, random2);
        scanf("%d", &answer);
    
    
        if (answer == (random1-random2)){
            printf("Congratulations\n\n");
                *reward++;// tells result whether to add a pt or not
            }
            else{
                printf("Incorrect\n\n");}
    
    
    
    }
    Last edited by chucker; February 19th, 2012 at 04:54 PM.

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++/C conversion to just C

    Quote Originally Posted by chucker View Post
    Any advice on how to implement the pointers as a replacement? I know the basics of them, but not quite how to apply them to my existing program
    Well, if you know the basics, then that would include knowing how to pass pointers to functions.

    Your book doesn't discuss passing pointers to functions? As a matter of fact, look at your current code:
    Code:
      scanf("&#37;d", &MenuChoice); // user input
    What do you think is happening there in red?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 19th, 2012 at 04:52 PM.

  15. #15
    Join Date
    Feb 2012
    Posts
    68

    Re: C++/C conversion to just C

    okay i decided to take a different approach with globals instead of pointers. It compiles, but the screen is black. Any input in this case?

    Code:
    
    int menu();
    int addition(int reward); // function to return truth values
    int subtraction(int reward);
    
    int main()
    {
    
    
    
    int Result, Finished = 0;
    int choice, score = 0;
    int reward = 0;
    
    while(Finished != 1); // while finished is not true
    {
        printf("\nscore: &#37;d", score); // print the score
    
    
    
    {
    choice = menu(); // calls for user input and enters it to switch
    
    
    
    
    
        switch  (choice){ // accesses the functions
                case 0: Finished = 1;                     break;
                case 1: Result = addition(reward);           break;
                case 2: Result = subtraction(reward);        break;
                default : printf("Error in input\n\n");      break;
        }
        if(Result == 1){
            score += reward; // score = score + reward
            }
    
    }
    }
    }
    int menu() //main menu that gets called and allows fr user input
    {
        int MenuChoice;
            printf("\n");
            printf("Enter 1 for addition\n");
            printf("Enter 2 for Subtraction\n");
            printf("Enter 0 to quit\n");
            printf("-----------------\n");
            printf("Choice: ");
        scanf("%d", &MenuChoice); // user input
        return MenuChoice; //return the choice to choice to select a case
    }
    
    
    
    int addition(int reward) //function that is called by the users selection
    {
    
    int answer;
    int random1, random2;
    
    
    
    srand(time(NULL)); //generates fresh numbers each time.
    
        random1 = 1+(rand()%99); // from 1-99
        random2 = 1+(rand()%99); // from 1-99
    
    
        printf("\n%d - %d = ", random1, random2); //code to format the random math problem
        scanf("%d", &answer); // user input of answer
    {
    
    
    
    
    if (answer == (random1+random2)){
            printf("Congratulations\n\n");
                return reward = 1;// tellsesult whether to add a pt or not
            }   //if user is correct than the score is incremented
        else{
            printf("Incorrect\n\n");} //if not than the score is not incremented
    return 0;
    
    
    }
    }
    
    
    
    int subtraction(int reward) // follows same model as previous function, but instead subtraction.
    {
    
    
        int answer;
        int random1, random2;
    
    
    
    
    srand(time(NULL));
    
        random1 = 1+(rand()%99); // from 1-99
        random2 = 1+(rand()%99); // from 1-99
    
    
    
        printf("\n%d - %d = ", random1, random2);
        scanf("%d", &answer);
    
    
        if (answer == (random1-random2)){
            printf("Congratulations\n\n");
                return reward = 1;// tells result whether to add a pt or not
            }
            else{
                printf("Incorrect\n\n");}
    
    return 0;
    
    }

Page 1 of 3 123 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured