954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Homework using the "if" statement

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;

}
Sundayy
Newbie Poster
10 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Do NOT use spaces in the variable names... It's prohibited.

Dha...1
Newbie Poster
13 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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.

Dha...1
Newbie Poster
13 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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

ninjatalon
Junior Poster in Training
81 posts since Feb 2011
Reputation Points: 11
Solved Threads: 3
 

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;

}
Sundayy
Newbie Poster
10 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

What is the point with this line?

if (X1>Y);
Akill10
Posting Pro
575 posts since Sep 2010
Reputation Points: 115
Solved Threads: 80
 

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.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

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 <strong><<</strong> "What is X1/60:\n";

This is the one with the error: cout <strong>>></strong> "What is (X3*360):\n";

What's different?
You've used the "streamextraction" 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.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 
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.

spoonlicker
Junior Poster
127 posts since Feb 2011
Reputation Points: -9
Solved Threads: 3
 

>>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.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

>>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.

spoonlicker
Junior Poster
127 posts since Feb 2011
Reputation Points: -9
Solved Threads: 3
 
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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
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.

spoonlicker
Junior Poster
127 posts since Feb 2011
Reputation Points: -9
Solved Threads: 3
 

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.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 
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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

> 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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

> 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.

spoonlicker
Junior Poster
127 posts since Feb 2011
Reputation Points: -9
Solved Threads: 3
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

ninjatalon
Junior Poster in Training
81 posts since Feb 2011
Reputation Points: 11
Solved Threads: 3
 

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

Red Goose
Junior Poster
116 posts since Jan 2011
Reputation Points: 43
Solved Threads: 21
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
 
View similar articles that have also been tagged: