Very simple program with unexpected output

This is a discussion on Very simple program with unexpected output within the C++ Programming forums, part of the General Programming Boards category; Hello, I am writing a very simple program to print out multiplication tables. (I always did stink at maths). I ...

  1. #1
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24

    Very simple program with unexpected output

    Hello,

    I am writing a very simple program to print out multiplication tables. (I always did stink at maths).

    I think I have it written correctly but I get unexpected out put:

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int multiple;
    int main()
    {
        cout << "enter multiple: ";
        cin >> multiple;
        for (int i = 1 ; i < 12 ; i++)
        {
            cout << i << " x " << multiple << i*multiple << endl;
    
    
        }
    
    
    
    
    
    
    
    
        return 0;
    }
    if I enter 8 then I get 88 as first output, then 816 and so on with an extra 8 at the start. This is the same with any other table.

    This will be something so basic I'll feel stupid but it's good to learn,
    thanks in advance

  2. #2
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24
    sorry please disregard!

    It was so simple even I worked it out

    Apologies for wasting peoples time

  3. #3
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    Don't worry about it; that's not wasting anyone's time. By the way, is there any reason why you made int multiple; a global variable? Global variables should generally be avoided like the plague.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Just compiled and run your project(i only included a '=' at output).
    Output :
    Code:
    enter multiple: 8
    1 x 8 = 8
    2 x 8 = 16
    3 x 8 = 24
    4 x 8 = 32
    5 x 8 = 40
    6 x 8 = 48
    7 x 8 = 56
    8 x 8 = 64
    9 x 8 = 72
    10 x 8 = 80
    11 x 8 = 88
    That is what you want,right?

    As for the global variables i agree with antred.

  5. #5
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24
    yes, it was what I wanted except for it ending at x11 (I wanted it to get to 12 but that was simple to fix). The point about global variables is understood. I just used one here as it is the only one in play and it didn't really matter. I use local ones when I'm doing anything else

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Draylath View Post
    yes, it was what I wanted except for it ending at x11 (I wanted it to get to 12 but that was simple to fix). The point about global variables is understood. I just used one here as it is the only one in play and it didn't really matter. I use local ones when I'm doing anything else
    So you used a global variable without a reason.Try not next time

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unexpected output
    By juice in forum Windows Programming
    Replies: 6
    Last Post: 03-10-2012, 11:13 AM
  2. Unexpected output
    By juice in forum C Programming
    Replies: 24
    Last Post: 11-18-2011, 11:18 PM
  3. Unexpected output
    By GolDRoger in forum C Programming
    Replies: 9
    Last Post: 11-18-2011, 02:49 PM
  4. unexpected output
    By crash88 in forum C Programming
    Replies: 2
    Last Post: 05-16-2006, 10:03 PM

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