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?
// Ex. 4.19 //
#include <iostream>
usingnamespace 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?
#include <iostream>
usingnamespace 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;
}
elseif (large2 < num )
{
large2 = num;
}
++counter;
}
cout << "Largest number: " << large1 << endl;
cout << "Second largest: " << large2 << endl;
system("pause");
}
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?
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.
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>
usingnamespace 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");
}
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.
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.