C++/C conversion to just C - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 42

Thread: C++/C conversion to just C

  1. #16
    Join Date
    Apr 1999
    Posts
    27,434

    Re: C++/C conversion to just C

    Quote Originally Posted by chucker View Post
    okay i decided to take a different approach with globals instead of pointers.
    Why?

    The way you learn something is to write a small program instead of trying to shoehorn "learning" code into a larger, more complex program.
    Code:
    #include <stdio.h>
    
    void ptrTest(int *p)
    {
        *p = 20;
    }
    
    int main()
    {
       int x = 10;
       printf( "The value of x = &#37;d\n", x );
       ptrTest( &x );
       printf( "The value of x = %d\n", x );
    }
    OK, so what don't you understand about the above program? Do you see that the value of x changes to 20?

    That's why I pointed you to your own code with scanf() as a usage of pointers. Look at the call to ptrTest -- does it look familiar?

    Also, global variables should be avoided.
    It compiles, but the screen is black. Any input in this case?
    Please format your code a little better than you have now.
    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: %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;
    }
    Also, are you using a debugger to debug your code?

    Regards,

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

  2. #17
    Join Date
    Apr 1999
    Posts
    27,434

    Re: C++/C conversion to just C

    Quote Originally Posted by chucker View Post
    It compiles,
    Now we get into the danger of using C without properly learning it, and the reason why I stated to learn C from scratch, as if you didn't know C++. Yes, C++ borrows most basic syntax from 'C', but this is where you get into trouble, since C++ doesn't have the issues I'll explain below:
    Code:
        printf("\nscore: &#37;d", score); // print the score
    Where do you #include <stdio.h>? You didn't include it. So what happens when you compile the code using 'C'? The compile is successful, but you wind up potentially shooting yourself in the foot.

    The 'C' compiler assumes the function returns an int, and that the parameters you're providing are correct. But this is the danger -- if you call functions that do not return an int, or you call functions where the parameters are actually different types and/or number of parameters, then your code can exhibit undefined behaviour.

    For example, this code compiles, but what will it do?
    Code:
    int main()
    {
       int x;
       x = sqrt( "abc123", 343, 53243 );
    }
    This 'C' code compiles also . The problem is that in reality, the sqrt() function returns a double, and takes a single double parameter. So what happens when you run this program? Who knows? Maybe your computer blows up. The difference in C++ is that there is no way the above code could have compiled, but C says "OK, I guess you know what you're doing".

    The way you avoid this is to introduce the prototype to sqrt() and the easiest way to do that is to #include <math.h>
    Code:
    #include <math.h>
    
    int main()
    {
       int x;
       x = sqrt( "abc123", 343, 53243 );
    }
    Now the code will not compile successfully in 'C', since the prototype for sqrt() is in math.h, and the 'C' compiler can clearly see you're calling the sqrt() function incorrectly.

    So by not #including all the proper headers and/or prototyping your functions, your program can easily become ill-formed. That's why I've always emphasized to posters posting 'C' code that they must post all header files that are included -- no shortcuts, no snippets, etc.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 19th, 2012 at 05:20 PM.

  3. #18
    Join Date
    Feb 2012
    Posts
    68

    Re: C++/C conversion to just C

    That was a copy and paste fault, stdio.h was there. thanks for the help so far, i am learning things. I went back to pointers and thought i applied them more correctly, but the screen is still just blank when i run it. I use codeblocks, so yes about the debugger.

    Also thanks for the organization, will model the rest of my programs from that.



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    
    
    
    
    int menu();
    void addition(int *Preward); // function to return truth values
    void subtraction(int *Preward);
    
    int main()
    {
        int Finished = 0;
        int choice, score = 0;
        int reward = 0;
    
        while(Finished != 1); // while finished is not true
        {
            printf("\nscore: %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(&reward);                break;
                case 2: subtraction(&reward);             break;
                default : printf("Error in input\n\n");   break;
                }
                if(reward == 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
    }
    
    void addition(int *Preward) //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");
                *Preward = 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
    
        }
    }
    
    void subtraction(int *Preward) // 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");
            *Preward = 1;// tells result whether to add a pt or not
        }
        else{
            printf("Incorrect\n\n");
        }
    
    }

  4. #19
    Join Date
    Apr 1999
    Posts
    27,434

    Re: C++/C conversion to just C

    Quote Originally Posted by chucker View Post
    That was a copy and paste fault, stdio.h was there.
    That's why it's important to make sure everything in your post is included, and especially the case if it's a 'C' program.
    the screen is still just blank when i run it. I use codeblocks, so yes about the debugger.
    Single-step through your program using the debugger and see where it goes wrong.

    Regards,

    Paul McKenzie

  5. #20
    Join Date
    Feb 2012
    Posts
    68

    Re: C++/C conversion to just C

    oh shoot i always thought debugger just referred to compile errors.

    I am trying to use it. I clicked debug and my program turned on immediatly blank. How do i get the option to step through it? its greyed out on the debugger bar.

  6. #21
    Join Date
    Feb 2012
    Posts
    68

    Re: C++/C conversion to just C

    I dont see a reason why the first printf statement cannot be displayed. Finished is clearly not equal to 1 yet so "score: 0" should be able to display at least. Its compiling with zero errors i mean cmon.

  7. #22
    Join Date
    Aug 2009
    Posts
    439

    Re: C++/C conversion to just C

    You need to set a break point. From there, you can step through your code.

  8. #23
    Join Date
    Feb 2012
    Posts
    68

    Re: C++/C conversion to just C

    I put a break point at the end of my main function. Nothing happens when I click debug except a black execution window. It doesnt give me the option to step thru still, its greyed out

  9. #24
    Join Date
    Apr 1999
    Posts
    27,434

    Re: C++/C conversion to just C

    Quote Originally Posted by chucker View Post
    I put a break point at the end of my main function. Nothing happens when I click debug except a black execution window. It doesnt give me the option to step thru still, its greyed out
    Set a break point at the beginning of main() (the first executable line), not at the end.

    What is probably happening is that your program is running all the way until the end, which is where you set the breakpoint.
    Its compiling with zero errors i mean cmon.
    Compiling with no errors has nothing to do with how your program will run.

    Compiling with no errors means that your program is syntactically correct -- it doesn't mean your program is logically correct.

    Regards,

    Paul McKenzie

  10. #25
    Join Date
    Feb 2012
    Posts
    68

    Re: C++/C conversion to just C

    set break point right after main. absolutely nothing happens, nothing updates on the watches. Zip.

  11. #26
    Join Date
    Apr 1999
    Posts
    27,434

    Re: C++/C conversion to just C

    Quote Originally Posted by chucker View Post
    set break point right after main. absolutely nothing happens, nothing updates on the watches. Zip.
    Did you build your program using the correct debugging options? The debugger that is being used is gdb, and if you don't build your program with debug information, the debugger isn't going to work.

    Regards,

    Paul McKenzie

  12. #27
    Join Date
    Aug 2009
    Posts
    439

    Re: C++/C conversion to just C

    If you set a break point at the beginning and the program doesn't pause at that break point, then you have done something wrong. You should just be able to press F8 in Code::Blocks to run your program in debug mode. If you don't have that option, you need to create a project and add your file to that project.

  13. #28
    Join Date
    Feb 2012
    Posts
    68

    Re: C++/C conversion to just C

    its on gdb, its apart of a C project file, no matter where I put break points it wont give me any debug info in watches.

  14. #29
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,315

    Re: C++/C conversion to just C

    Did you set any variables to watch?
    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

  15. #30
    Join Date
    Feb 2012
    Posts
    68

    Re: C++/C conversion to just C

    at the beginning of main when i declare all of those ints like "int choice" I right clicked them and selected find implementation/declaration and it says choice is not found, along with all the others.



    Code:
    /*---------------------------------------------------------------
     * Programmer: Charles Cunningham
      * Date: 2/5/2012
     * Name: Hw#1
     * Description: This program generates addition or subtraction problems
     based on the users choice. Then the user must guess the correct answer. If correct the user gets a point added to his score
     ---------------------------------------------------------------*/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    
    
    
    
    
    int menu();
    void addition(int *Preward); // function to return truth values
    void subtraction(int *Preward);
    
    int main()
    {
        int Finished = 0;
        int choice, score = 0;
        int reward = 0;
    
        while(Finished != 1); // while finished is not true
        {
            printf("\nscore: %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(&reward);                break;
                case 2: subtraction(&reward);             break;
                default : printf("Error in input\n\n");   break;
                }
                if(reward == 1){
                    score += reward; // score = score + reward
                }
            }
        }
        return 0;
    }
    
    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 *Preward) //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");
                *Preward = 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
    
        }
    }
    
    void subtraction(int *Preward) // 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");
            *Preward = 1;// tells result whether to add a pt or not
        }
        else{
            printf("Incorrect\n\n");
        }
    
    }

Page 2 of 3 FirstFirst 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center