newbie: find largest number

Hello, im new in c++. im learning by myself online and from a textbook, using dev-c++ as my compiler.

right now im trying to do exercises from a textbook (C++ how to program, deitel), and currently im stuck on 2 questions.

first one, is about finding the two largest numbers out of 10 integers entered by user. here is what i got, but i think my second largest number is still wrong, and for some reason my last two couts dont print. any suggestion?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Ex. 4.19 //

#include <iostream>
using namespace std;

int main (){
    
    int counter=0, num, large1=0, large2=0;
    
    cout << "Enter an integer: ";
    
    while (counter<10){
          cin >> num;
          if (large1 < num)
             large1 = num;
          if (large2 < num && large2 < large1)
             large2 = num;
          ++counter;
    
          }
    cout << "Largest number: " << large1;
    cout << "\nsecond largest: " << large2;  
    cin.get();
}                                     
    


the second problem:
im trying to print a diamond by "*" and " "(space) using for statement. but i can't figure out the logic for it. i could print a triangle by nested for, but can't do the diamond, any clue?

Thanks!
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;


int main ()
{
    int counter = 0; 
    int num = 0;
    int large1 = 0; 
    int large2 = 0;
    
    while (counter < 10)
    {
        cout << "[" << counter << "] Enter an integer: ";  
        cin >> num;
        cin.get();  // This just clears the new line from the stream
                    // don't worry about it too much at the moment.

        if (large1 < num)
        {
            large2 = large1;
            large1 = num;
        }
        else if (large2 < num )
        {
            large2 = num;
        }
        ++counter;
    }
    cout << "Largest number: " << large1 << endl;
    cout << "Second largest: " << large2 << endl;  
    
    system("pause");
} 
Last edited on
what exactly is the difference between cin.get() and system("pause")? i read somewhere that cin.get() supposed to get the effect of system("pause"), that's why i have always include that line in every script i wrote, though sometimes it doesnt pause.

now i know system("pause") will do the trick, i will keep using that from now on :p

now what confuses me is, why sometimes the window will disappear after it runs, sometimes doesnt?
closed account (z05DSL3A)
Okay, system() basically asks the operating system to run a program, so system("pause") asks the OS to run pause.exe. pause.exe displays ‘press any key to continue’ and then waits for a key to be pressed. Control is then passed back to your program.

Onto cin.get(), gets a character from the stream (cin) and returns its value (casted to an integer). If there is ‘junk’ in the stream a call to get will return and your program will carry on.

Does that help?
thanks greywolf! :D

i have another question, where I have to find the product of odd integers from 1 to 15. im trying to do it with 2 approaches: one using for loop with increment of 2 (which i got it to work with correct answer), and another approach i tried doing is by using modulus, but somehow i got wrong answer.

could someone check what's wrong with my code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main(){
    int odd=1, test;
    for (int x=1; x<16; x++){
        test = x%2;
        if (test=1){
           odd = odd*x;
        }
        else {
             break;
        }
    }
    cout << "Product of odd integers from 1 to 15: "<<odd<<endl;
    system("pause");
}
Last edited on
closed account (z05DSL3A)
This is just a bit of advice for posting.
If you have a new question (not related to the original question), it is better to start a new thread, because you may restrict your 'viewing' audience to only the people currently taking part in the current thread if you don't.
Last edited on
Your problem is the "if (test=1){" part, I think. It should be "if (test==1){" because = is for assigning one value to another and == is for comparing two values.

As Grey Wolf mentioned, if you have a completely new question, please start a new thread. It helps with organization (less scrolling), and it helps future programmers with the same problem because they can search.

I hope this helps! ^_^

rpgfan
I think you should use "continue" instead of "break" in your program. Otherwise, it will break when x=2.
nnzzyy wrote:
> I think you should use "continue" instead of "break" in your program.
> Otherwise, it will break when x=2.

True, though technically, that means the else statement is unnecessary since it will automatically continue. ^_^
Last edited on
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
...
    for (int x = 1; x < 16; x++)
    {
        if ( (x % 2) != 0)
        {
           odd *= x;
        }
    }
...
Last edited on
thanks a lot! :D you all are right, i tried your advices, they all solve the problem :D thanks

i will create new topic if there is more new question.
Topic archived. No new replies allowed.