Hello again:
I am having trouble declaring my variables. Can you please show me what I am doing wrong. The homework problem ask for me to use the "if" statement also, kinda having a few problems with that. Please see the question and my code below.

Thank you for your time.

Homework 3a:
Do using the if statement
Question:
Write, compile and run a C++ program named hw3a.cpp that reads in one integer that represents a total number of seconds and then breaks that number down to display the equivalent number of hours, minutes and seconds, all properly labeled.

Have your program ONLY do the calculation if the user enters a number LESS than 50,000 hours. If he enters 50,000 or more print an error message and quit.

Sample run:

Input a whole number of seconds less than 50,000:
7683
There are 2 hours, 8 minutes and 3 seconds in 7683 seconds

Sample run:

Input a whole number of seconds less than 50,000:
76099
Sorry, you were asked to enter a number less than 50,000


Source Code

//Variables:
//X1 = Total Number of Seconds
//X2 = Number of Minutes = X1/60
//X3 = Number of Hours = X1/360
//X4 = Number of Seconds = X3*360
//Y = 50000

#include  <iostream>
using namespace std;
int main()
{

    int Total Number of Seconds;
    int Number of Minutes;
    int Number of Hours;
    int Number of Seconds;
    int Y, X1, X2, X3, X4;

if (X1>Y):

cout << "What is the total number of seconds:\n";
cin >> totalnumSeconds;
cout << "What is the number of hours:\n";
cin >> numHours;
cout << "What is the number of minutes:\n";
cin >> numMinutes;
cout >> "What is the number of seconds:\n";
cin >> numSeconds;

if (X1>Y):

cout << "ERROR:" << endl;

return 0;

}

Edited 5 Years Ago by Sundayy: forgot tags

And, your program is wrong if this is the final logic that you have applied.
You have to CALCULATE. Not INPUT the hours, minutes. Only seconds are to be input and other things are to be calculated.

first you need make a variable where the user inputs the total of numbers
then you need to ask is the variable higher than 50 thousand if yes run your if statements to find hours mins and seconds, if not then display error message and exit

Here is my second attempt, I am now getting on line 27, an error; no match for 'operator >>' in std::cout >> "What is (X3*360):\012"'.
Can you please tell me how to fix it, see my code below.

Thank You

CODE

//Variables:
//X1 = Total Number of Seconds
//X2 = Number of Minutes = X1/60
//X3 = Number of Hours = X1/360
//X4 = Number of Seconds = X3*360
//Y = 50000

#include  <iostream>
using namespace std;
int main()
{

    int X1,X2,X3,X4;
    int Y=50000;

if (X1>Y);


cout << "What is X1:\n";
cin >> X1;
cout << "What is X1/360:\n";
cin >> X3;

cout << "What is X1/60:\n";
cin >> X2;

cout >> "What is (X3*360):\n";
cin >> X4;

if (X1>Y);

cout << "ERROR:" << endl;

return 0;

}

Instead of naming your variables something doesn't make any sense, how about naming them something like totalseconds, hours, minutes, seconds.

Also, like one of the prior posters said, you have to calculate them WITHIN the program, not have the user enter the calculated value.

Here is my second attempt, I am now getting on line 27, an error; no match for 'operator >>' in std::cout >> "What is (X3*360):\012"'.
Can you please tell me how to fix it, see my code below.

Thank You

CODE

//Variables:
//X1 = Total Number of Seconds
//X2 = Number of Minutes = X1/60
//X3 = Number of Hours = X1/360
//X4 = Number of Seconds = X3*360
//Y = 50000

#include  <iostream>
using namespace std;
int main()
{

    int X1,X2,X3,X4;
    int Y=50000;

if (X1>Y);


cout << "What is X1:\n";
cin >> X1;
cout << "What is X1/360:\n";
cin >> X3;

cout << "What is X1/60:\n";
cin >> X2;

cout >> "What is (X3*360):\n";
cin >> X4;

if (X1>Y);

cout << "ERROR:" << endl;

return 0;

}

This is a valid line: cout [B]<<[/B] "What is X1/60:\n"; This is the one with the error: cout [B]>>[/B] "What is (X3*360):\n"; What's different?
You've used the "stream extraction" operator (operator>>) which is an input operator on an output stream. Output streams don't have input operators. You need to use the "stream injection" operator (operator<<) on an output stream.

Instead of naming your variables something doesn't make any sense, how about naming them something like totalseconds, hours, minutes, seconds.

The point of being a programmer is to name a variable any thing the programmer wants, not to hear how their named variables are wrong.

The error in line 27 is using >> instead of << for cout.

And before was because you're using space between variable names.

Any thing else, just ask.

>>The point of being a programmer is to name a variable any thing the programmer wants, not to hear how their named variables are wrong.
To a reasonable extent, yes. The problem is, if you use a meaningless name, it makes it very difficult to know what the value in the variable is supposed to represent.

If you have a program that calculates the Pythagorean Theorem, the variable names "side1Length", "side2Length", and "hypotenuseLength" are more meaningful than 'x', 'y', and 'z'. If you go back to your program later, let's say a year from now, will you be able to easily tell what 'x', 'y' and 'z' represent? Probably not, you'll either have to hope that the author provided useful comments or you'll have to track them down through the code and see how they're used to figure it out.

This is part of a concept commonly called "self-documenting code". The idea is to make your code as readable as possible. Ideally, your code is so easily readable that many comments can be eliminated as redundant.

Edited 5 Years Ago by Fbody: n/a

>>The point of being a programmer is to name a variable any thing the programmer wants, not to hear how their named variables are wrong.
To a reasonable extent, yes. The problem is, if you use a meaningless name, it makes it very difficult to know what the value in the variable is supposed to represent.

If you have a program that calculates the Pythagorean Theorem, the variable names "side1Length", "side2Length", and "hypotenuseLength" are more meaningful than 'x', 'y', and 'z'. If you go back to your program later, let's say a year from now, will you be able to easily tell what 'x', 'y' and 'z' represent? Probably not, you'll either have to hope that the author provided useful comments or you'll have to track them down through the code and see how they're used to figure it out.

This is part of a concept commonly called "self-documenting code". The idea is to make your code as readable as possible. Ideally, your code is so easily readable that many comments can be eliminated as redundant.

Again, it's their choice. Code doesn't always have to be reused, you know? Not every one is going to be working for Microsoft, some people just do it for personal reasons and, in that case, the code can be memorized well or at least should be.

I just use unique names I come up with and when I pass by the names I automatically know what it'll do because "I" came up with it.

Comments
Stop provoking people
Refuting a good suggestion by describing your own poor habits isn't helping anyone.

I automatically know what it'll do because "I" came up with it.

Until you leave the code and come back in a year and it looks like it was written by someone else. Then you're SOL.

Until you leave the code and come back in a year and it looks like it was written by someone else. Then you're SOL.

And give me any one reason, big or small, as to why I'd leave code sitting for one year and not use it, delete it or work around with it?

Please...Explain that to me.

Comments
This is turning into a hijack.

You have been using the executable, and the program has been working, but you've recently found a bug that you want to fix. You get back to your code to fix it and you now have no idea what variable/function you're looking for because none of the names make sense. As a result, you have to completely reverse-engineer the software to figure out what section of code to look at instead of just going to the appropriate function and making the required change.

I think at this point it would be a good idea to start a new thread if you want to discuss the validity/value of good variable names further.

Edited 5 Years Ago by Fbody: n/a

And give me any one reason, big or small, as to why I'd leave code sitting for one year and not use it, delete it or work around with it?

You might write yourself a tool and use it but not need to examine the code. But then a subtle bug finally surfaces one day or you might want to add a feature. If all you write are throwaway programs then be as sloppy as you want. But if your programs are meant to last, then the code should be written to last as well.

> And give me any one reason, big or small, as to why I'd leave code sitting for one year and not use it, delete it or work around with it?
Any working programmer is going to have plenty of code around that they haven't looked at in over a year. I have production code that I probably haven't looked at in over five years.

I can absolutely guarantee you'll understand why meaningful variable names are not just a trivial nitpick suggestion if you have to work on some code you wrote years ago.

> And give me any one reason, big or small, as to why I'd leave code sitting for one year and not use it, delete it or work around with it?
Any working programmer is going to have plenty of code around that they haven't looked at in over a year. I have production code that I probably haven't looked at in over five years.

I can absolutely guarantee you'll understand why meaningful variable names are not just a trivial nitpick suggestion if you have to work on some code you wrote years ago.

I don't work that way though.

I always begin and end or quit. I don't leave code sitting around for no reason, and if I'm done with it I'm done.

Either that or I'll lay off for a short-time but not long enough for me to forget what I've done. And what makes you think I'd wrote code and leave it there for no use? I'd either write it and use the program it produces or fail to complete it and delete it.

Even if you don't have long-term code as a consideration, using clear and meaningful variable names is useful in maintaining focus and understanding of what any section of code is doing.

As already explained above, using a name like "hours" instead of "X3" makes the logic a lot easier to follow - even for the person writing it.

And let's give this thread back to the OP now. We've interjected more than enough on variable naming.

To OP you are making an if statement if x1 is greater than y
But you x1 doesn't have a value yet.
You should ask the user what x1 is first and then compare if x1 is greater than y

Your if statements aren't doing anything to begin with. You may want to carefully reexamine if statement implementation.

I am with using appropriate variable names.
They make debugging better, clearer program (and in this case, it's a homework!)
And, one should keep habit of this right from the beginning. Later, when you've developed, it'll be a bit difficult to adapt to the new style....

i totally agree with appropriate variable naming too. i far too often find myself stuck debugging only if i had just used better variable names the problem would be easier to isolate. It also helps other people read and understand your code when you ask for their help.

i totally agree with appropriate variable naming too. i far too often find myself stuck debugging only if i had just used better variable names the problem would be easier to isolate. It also helps other people read and understand your code when you ask for their help.

You could always put comments next to the variable news if you do have bad memory and forget what they would do or what they're for.

Edited 5 Years Ago by spoonlicker: n/a

Closing thread as it is going off topic thanks to spoonlicker. You now have official warning about hijacking other people threads.

@Sundayy if you still need help with your work please create new thread and I'm sure people who been helping you and other who are willing to help will help.

This article has been dead for over six months. Start a new discussion instead.