Like Tree1Likes
  • 1 Post By tabstop

Jumping into C++ Question - Getting EAX Registery value in my program

This is a discussion on Jumping into C++ Question - Getting EAX Registery value in my program within the C++ Programming forums, part of the General Programming Boards category; Hello, I am having an issue with my program showing my eax registry value. I have been googling for 6 ...

  1. #1
    Registered User
    Join Date
    Oct 2013
    Location
    Everywhere
    Posts
    4

    Jumping into C++ Question - Getting EAX Registery value in my program

    Hello,
    I am having an issue with my program showing my eax registry value. I have been googling for 6 hours now and have not found the answer. Can someone help? Code is below. I know exactly where the value is being added and can even change it to 0, but that exits my loop. Should I be looking to change that value to something different. This is not homework just trying to learn C++. I found the error by debugging and pulling up the different debugging windows. Thank you in advance.

    Code:
    #include <iostream>
    using namespace std;
    
    int add ( int x, int y );
    int sub ( int x, int y );
    int multiply ( int x, int y );
    int divide ( int x, int y );
    int beerbottles ( int b );
    
    
    int main ()
    {
        int i, b, x, y, z;
    
        while ( i != 0 )
        {
            cout << "Choose an option below" << endl;
            cout << "1. Hear a song." << endl;
            cout << "2. Calculator." << endl;
            cout << "Option:";
            cin >> i;
            cout << endl;
    
            if ( i == 1 )
            {
                cout << "1) Add" << endl;
                cout << "2) Subtract" << endl;
                cout << "3) Multiply" << endl;
                cout << "4) Divide" << endl;
                cin >> z;
    
                if ( z == 1 )
                {
                    cout << "1st number to add: ";
                    cin >> x;
                    cout << "2nd number to add: ";
                    cin >> y;
                    int result = add( x, y );
                    cout << "Total is: " << result;
                }
                if ( z == 2 )
                {
                    cout << "1st number to subtract: ";
                    cin >> x;
                    cout << "2nd number to subtract: ";
                    cin >> y;
                    int result = sub( x, y );
                    cout << "Total is: " << result;
                }
                if ( z == 3 )
                {
                    cout << "1st number to multiply: ";
                    cin >> x;
                    cout << "2nd number to multiply: ";
                    cin >> y;
                    int result = multiply( x, y );
                    cout << "Total is: " << result;
                }
                if ( z == 4 )
                {
                    cout << "1st number to divide: ";
                    cin >> x;
                    cout << "2nd number to divide: ";
                    cin >> y;
                    int result = divide( x, y );
                    cout << "Total is: " << result;
                }
            }
            if ( i == 2 )
            {
                    cout << "How many beer bottles are on the wall? ";
                    cin >> b;
                    cout << beerbottles( b );
            }
        }
    
    }
    
    int add ( int x, int y )
        {
            return x + y;
        }
    int sub ( int x, int y )
        {
            return x - y;
        }
    int multiply ( int x, int y )
        {
            return x * y;
        }
    int divide ( int x, int y )
        {
            return x / y;
        }
    int beerbottles( int b )
    {
        while ( b > 0 )
        {
            cout << b << " bottles of beer on the wall,  " << b << " bottles of beer " << endl;
            --b;
            cout << "Take one down, pass it around, " << b << " bottles of beer on the wall." << endl;
            cout << endl;
        }
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    I do not see eax anywhere in this program - but in line 15 you access uninitialized variable - you should get warning about it.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,283
    eax is a processor register that the compiler is using to do calculations with, and you normally only see it being used when looking at the machine language during debugging.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by juan.valdez View Post
    I am having an issue with my program showing my eax registry value.
    If you care about EAX registry, why did you show C++ code instead of assembly? If you come from low level programming background, you have to know that the compiler may decide to use a different registry if EAX isn't spare. Which registry will be chosen highly depends on the compiler being used.

    It will be better for you if you stop thinking about assembly at all, at least for now.
    I never put signature, but I decided to make an exception.

  5. #5
    Registered User
    Join Date
    Oct 2013
    Location
    Everywhere
    Posts
    4
    Here is what I am seeing. When I run the code it runs just fine. The inputs are backwards when choosing a song or calculator, but if you choose song and choose a number for the bottles of beer on the wall you will see the number pop up after line 73. That number is my eax registry number. So I know the problem is at line 73 but do not know what I am doing for it to grab that number. I suspect that it is from the function beerbottles and possibly my int b is grabbing the value but not sure how to fix it. By the way I am using codeblocks as my editor and minigw as my compiler.

    vart - not sure what you mean seeing on line 15 I have my while loop. On line 15 I have "while ( i != 0 )", but if you notice I have i initialized right above there so not sure where you are going with this line of troubleshooting.

    rcgldr - I see the number by just clicking on the exe file created by my compiler at the end of my program on line 73.

    kmdv - not sure where you are going with assembly, sounds like high level stuff to me.

    Remember people I put the title of my post as Jumping into C++, which is a beginners book to coding. This is the reason I am posting on this forum. I am trying to learn and I have hit a snag so any help would be greatly appreciated.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,725
    Quote Originally Posted by juan.valdez
    On line 15 I have "while ( i != 0 )", but if you notice I have i initialized right above there so not sure where you are going with this line of troubleshooting.
    No, you did not initialise i "right above there". You merely declared i without specifying an initial value before you used it in that loop condition.

    Quote Originally Posted by juan.valdez
    I see the number by just clicking on the exe file created by my compiler at the end of my program on line 73.
    On line 73, you call a function and print its return value to standard output. The "clicking on the exe file created by my compiler" basically means "running your program". As such, you're talking about the output of your program, yet you call it the "eax registry value".

    Quote Originally Posted by juan.valdez
    not sure where you are going with assembly, sounds like high level stuff to me.
    Well, what do you mean by "eax registry value"? What does "eax" mean to you? Did you really mean "registry" rather than "register"?
    Last edited by laserlight; 10-19-2013 at 08:41 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2013
    Location
    Everywhere
    Posts
    4
    laserlight - did you run the program?

    I have included a png file to show you what I am talking about seeing no one is understanding what I am asking. I think I need to do something in the beerbottles function with the variable but not sure what.

    Name:  eax.jpg
Views: 97
Size:  64.1 KB

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The random number is probably because your beerbottles function is specified as returning an int "int beerbottles (int b)", yet you did not do so. Consequently, the computer chose a random number to return as the return value of that function. The line cout << beerbottles(b) then prints that return value (randomly chosen as above).
    laserlight likes this.

  9. #9
    Registered User
    Join Date
    Oct 2013
    Location
    Everywhere
    Posts
    4
    tabstop - thank you. you are not clear in your answer but I got it to work because I understood what you said. You helped sir or maam.

    I fixed my issue by changing my beerbottles function to a string and that solved it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 01-14-2013, 07:35 AM
  2. Registery read/write ?
    By Devil Panther in forum Windows Programming
    Replies: 3
    Last Post: 03-15-2004, 09:29 AM
  3. Windows 98 Registery... ?
    By jawwadalam in forum Tech Board
    Replies: 12
    Last Post: 10-16-2002, 02:37 PM
  4. Ms-Dos commands & The Win Registery
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-21-2002, 07:54 AM
  5. Registery with api?
    By ismael86 in forum Windows Programming
    Replies: 1
    Last Post: 05-14-2002, 10:28 AM

Tags for this Thread


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