Hi, I am very new to C++ and am trying to write a couple of programs. For the first one, I am trying to output a four-digit integer - one digit per line. I know that I have to put \n somewhere, but I can't figure out where to put them. Also, am I missing anything here other than \n? Here is what I have so far (they're outputting all on the same line):
#include <iostream>
using namespace std;
int main()
{
int num1;
int num2;
int num3;
int num4;
----Another one I am trying to write, I need the user to input an elapsed time for an event in hours, minutes, and seconds. Then I need it to output the elapsed time in seconds. How would I go about this one?
Well for the first one I can't figure out where to put the \n for each number. It should look like this:
1
2
3
4
but it looks like this right now:
1234
For the second one, it says:
"Write a C++ program that prompts the user to input the elapsed time for an event in hours, minutes, and seconds. The program then outputs the elapsed time in seconds."
I figured out how to do the second one I mentioned backwards so I ended up doing it that way.
****I still need help with this first one I mentioned though****
With the above program, where do I put in my \n (each one) at to make the numbers appear as follows:
Alright, I'm explaining this bad then. I'm sorry, I'm just completely new to this and have no idea how to word it. Here is exactly what I'm being told to do:
"Write a program that prompts the user to input a four-digit positive integer. The program the outputs the digits of the number, one digit per line. For example, if the input is 3245, the output is:
3
2
4
5"
I just don't understand what I am missing, how to write it, and where to put it inside the program I have already written. Again, I'm sorry for not being very clear with this. I do appreciate you even helping me! :)
Oh, I get it, now. Just have the user input a single number and make sure it's lower than ten thousand. Then just fool around with division by powers of ten and the modulo operator and you'll get it eventually.
Ahhh alright. Thanks so much. I see what you're saying. I had a feeling that I needed the modulus somewhere. Where would I put something like that? Would it be after this?:
cin >> num1;
cout << num1 << endl;
I guess I just don't understand where to put this stuff at. Our teacher is absolutely horrible so I'm over here trying to teach myself something brand new to me haha.
int main()
{
while(true)
{
int number;
cout << "Enter a 4-digit number:" << endl;
cin >> number;
//check to see if it is four digits using an if statement
if(( number > 9999 ) || ( number < 1000 ))
{
cout << "Please enter a 4-digit number." << endl;
break;
}
int thous = number / 1000;
int hund = ((number - (thous * 1000)) / 100);
int tens = (number - (thous * 1000) - (hund * 100)) / 10 ;
int ones = (number - (thous * 1000) - (hund * 100) - (tens * 10));
cout << thous << endl;
cout << hund << endl;
cout << tens << endl;
cout << ones << endl;
}
system("pause");
return 0;
}
I just used system("pause") so you can see the code when you debug it.
I actually pulled out the pencil and paper and tested it with actual numbers to get the code right.
This is because it does:
100 / 7 (int division) and gets
98 remainder 2
A little nitpic.........(it gets remainder 2 because 14*7==98)
1 2 3 4 5 6 7 8 9
int num;
cout<<"Enter a 4 digit integer number";
cin>>num;//num= 4631 if you hit those keys
cout<<num%10<<"\n";//prints remainder 1
num/=10;//assigns 463 to num
cout<<num%10<<"\n";;//prints remainder 3 on next line
num/=10;//assigns 46 to num
cout<<num%10<<"]n";//prints remainder 6 on next line
cout<num/10;//prints 4.6 converted to an integer (4)on next line
If these need reversing vertically can you imagine what is needed?
The problem is that I don't understand it. I don't understand why I'm putting numbers in. I got help from another site, I understood it a little more than this one, but it's not as simple as I think the teacher wants it to be. Here, you can check it out:
while(x>=1)
{
rem = num/x;
if(x<1000) rem -= (num/(x*10))*10;
x /= 10;
cout << rem << endl;
}
system("pause");
return 0;
}
OK, so like I said, I'm not sure why I put in numbers in this one either. We also haven't learned the whole "if" and "else" stuff yet, which is why I needed the \n. Also, the one above from the other forums doesn't have the \n in there & I don't see why it still works without it. I know she wants the \n in there.
I apologize, I do appreciate you helping me. I am only asking so many questions because the teacher is horrible & doesn't explain WHY we do things. And it's not just me, everyone who has this professor and others who have had her previously are all having or had the same problem with her style of "teaching." So, I guess what the problem is, is that I don't understand why I am doing things certain ways (putting in numbers, why my original program wasn't right, where & when to put in the \n, etc.)