Program to print 3 numbers in ascending order

This is a discussion on Program to print 3 numbers in ascending order within the C++ Programming forums, part of the General Programming Boards category; I have an assignment to write a code that will prompt the user to enter three integers, then it should ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    13

    Program to print 3 numbers in ascending order

    I have an assignment to write a code that will prompt the user to enter three integers, then it should print the integers in ascending order.

    The code that I have written has six errors & two warnings, but I don't know what to do.

    Here is the code:

    Code:
    #include <iostream>
    #include <conio.h>
    
    void main ()
    {
    clrscr();
    int array [3], t;
    for (int  x=0; x<3; x++)
    {
    	cout << "Enter integer number" << x+1 << " : " << endl;
    	cin<< array[x];
    }
    for (x=0; x<3; x++)
    {
    for (int y=0; y<2; y++)
    {
    	if(array[y]>array[y+1])
    	{
    		t=array[y];
    		array[y]=array[y+1];
    		array[y+1]=t;
    	}
    }
    }
    cout << "The integers in ascending order are : ";
    for (x=0;x<3;x++)
    cout << endl << array[x];
    getch();
    }

    And the errors/warnings are:

    C:\ascending.cpp(10) : error C2065: 'clrscr' : undeclared identifier
    C:\ascending.cpp(14) : error C2065: 'cout' : undeclared identifier
    C:\ascending.cpp(14) : error C2297: '<<' : illegal, right operand has type 'char [21]'
    C:\ascending.cpp(14) : error C2065: 'endl' : undeclared identifier
    C:\ascending.cpp(15) : error C2065: 'cin' : undeclared identifier
    C:\ascending.cpp(15) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
    C:\ascending.cpp(29) : error C2297: '<<' : illegal, right operand has type 'char [40]'
    C:\ascending.cpp(31) : warning C4552: '<<' : operator has no effect; expected operator with side-effect



    I'm kind of stuck.

    Oh, also, my teacher said to use scanf three times? I'm not sure if it matters that I didn't.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 'clrscr' : undeclared identifier
    It doesn't know what clrscr is. You don't really need it, though, so I would remove it.

    >> error C2065: 'cout' : undeclared identifier
    It doesn't know what cout is. In this case, the problem is that cout is from the std namespace. A simple solution is to add "using namespace std;" on a line under the #include's. Most of the other errors are because of the same issue, so make those two changes and try to compile again. You can then fix whatever error appear.

    >> Oh, also, my teacher said to use scanf three times?
    scanf is a C console input function. You used cin for your console input, which is a C++ option. You have a loop that calls cin, and that loop runs three times, so unless your teacher wants you to use scanf instead of cin, I think you're fine.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    13
    Okay, I corrected the errors.
    However, when my teacher said that the program should print the numbers in ascending order, did he mean that it should actually print out on paper?
    Because it doesn't do that.

    The code I have now is:

    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int  main ()
    {   int x;
        int array [3], t;
        for (x=0; x<3; x++)
        {
        cout << "Enter integer number: " << endl;
        cin >> array[x];
        }
        for (x=0; x<3; x++)
        {
            for (int y=0; y<2; y++)
            {
                if(array[y]>array[y+1])
                {
            t=array[y];
            array[y]=array[y+1];
            array[y+1]=t;
                }
            }
        }
        cout << "The integers in ascending order are : ";
        for (x=0;x<3;x++)
        {  
           cout <<"\n";
           cout <<array[x];
           cout << "\n";
        }
        getch();
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> did he mean that it should actually print out on paper?
    No, I'm pretty sure he meant output to the console window with cout (or printf or whatever) like you're doing now. The term print is often used to mean just output to the screen.

    >> The code I have now is:
    Looks like it does what the assignment asks for. I might not use <conio.h> if I could help it (you can replace getch() with a cin.ignore() and a cin.get()). It's also possible your sorting code could be improved, but the important part is that it works.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    13
    Okay, I tweaked a couple of things so this is what I have now:

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    int  main ()
    {   int x;
        int array [3], t;
        for (x=0; x<3; x++)
        {
        cout << "Enter integer number: " << endl;
        cin >> array[x];
        }
        for (x=0; x<3; x++)
        {
            for (int y=0; y<2; y++)
            {
                if(array[y]>array[y+1])
                {
            t=array[y];
            array[y]=array[y+1];
            array[y+1]=t;
                }
            }
        }
        cout << "The integers in ascending order are : ";
        for (x=0;x<3;x++)
        {  
           cout <<"\n";
           cout <<array[x];
           cout << "\n";
        }
        
        return 0;
    }

    And thanks for the clarification about "print."

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    The code that I have written has six errors & two warnings, but I don't know what to do.
    I have some general advice for you...

    Don't try to write the whole program at once. If you write the whole program before compiling, programming is going to be very frustrating.

    Always start-out with a little Hello World type program (or Hello Sort).... When that's working, add one or two lines of code, then test-compile and test-run.... Repeat 'till done.

    If you use this method, you won't get a bunch of errors at one time, and you will know exactly which lines have caused the error.

    This does NOT mean that you can write the code in "line-number order" a few lines at a time! (A program won't compile if you simply chop-off the 2nd half!)

    - In your case, I'd start with the user-input (after the Hello Sort version is working). Define your variables, and write the cin line. Then, throw-in an extra cout line to make sure the cin line is working. (You can take-out the extra cout lines when you're done.)

    - When that works, put the cin code in a loop.

    - When that works, write the cout loop. (The data isn't sorted yet.)

    - When that works, start working on the sorting code. You might want to start-out by just sorting the first two numbers... or just make one-pass without looping to make sure everythng is working as expected. Again, you can throw-in some extra cout statements to check the temporary/intermediate values.

    If you use this technique, programming will be a lot more fun! Professional programmers use similar techniques. Of course, they are not test-compiling every one or two lines. But, they are working on huge programs and they do write (and test) their code in managable chunks.

    I'm NOT saying that "trial-and-error" is a good programming technique. You should have a design and a plan, and you should understand what you are doing. But, you probably won't make it through beginning programming without some "experimentation". And, it is good programming practice to test your code as you are developing it.

    P.S.
    Just for future reference, C++ has a built-in sort() function in the <algorithm> header. Don't try to use it now. You'll probably cover it (and get an assignment on it) in the future. (There are some other sorting "tools" built-in too).
    Last edited by DougDbug; 11-08-2007 at 07:09 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 03:49 PM
  2. Need a little help with Cyclic Numbers Program
    By SlyMaelstrom in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2005, 05:01 PM
  3. Perfect Numbers C Program
    By SlayerBlade in forum C Programming
    Replies: 2
    Last Post: 08-28-2005, 05:11 PM
  4. Putting numbers in order
    By Queatrix in forum C++ Programming
    Replies: 7
    Last Post: 05-01-2005, 01:00 PM
  5. display numbers in order
    By mike in forum C++ Programming
    Replies: 2
    Last Post: 09-24-2001, 09:16 PM

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