You are here:

- Home
- Computing/Technology
- C/C++
- C++
- C++ HW ambiguous overload for 'operator cin >> operator

Advertisement

QUESTION: 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

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;

}

ANSWER: You can not have variable names in C++ like 'Total Number of Seconds'.In particular, white space is not allowed. Rename these so that they are valid C++ identifiers; eg. 'Total_Number_of_Seconds' would be fine.

int Total_Number_of_Seconds ;

std::cin >> Total_Number_of_Seconds ;

To break up the Total_Number_of_Seconds into hours, minutes and seconds:

Divide Total_Number_of_Seconds by 3600 (number of seconds in an hour) to get number of hours.

int hrs = Total_Number_of_Seconds / 3600 ;

Take the remainder seconds

int remainder = Total_Number_of_Seconds % 3600 ;

Divide remainder by 60 (number of seconds in a minute) to get number of minutes.

int mins = remainder / 60 ;

The remainder of this division gives the number of seconds.

int secs = remainder % 60 ;

---------- FOLLOW-UP ----------

QUESTION: Hi again:

I rewrote the code but, I am getting the following errors. Can you please tell me how to fix. Thank you in advance.

ERRORS:

Line|26|error: ambiguous overload for 'operator>>' in 'std::cin >> (Total_Number_of_Seconds / 3600)'|

Line|29|error: ambiguous overload for 'operator>>' in 'std::cin >> (Total_Number_of_Seconds % 3600)'|

Line|32|error: ambiguous overload for 'operator>>' in 'std::cin >> (Remainder / 60)'|

Line|35|error: ambiguous overload for 'operator>>' in 'std::cin >> (Remainder % 60)'|

CODE:

[B][U]CODE[/U][/B]

[CODE]//Variables:

//Total_Number_of_Seconds=50000

//Hours = Total_Numbers_of_Seconds/3600

//Remainder = Total_Number_of_Seconds%3600

//Minutes = Remainder/60

//Seconds = Remainder%60

#include <iostream>

using namespace std;

int main()

{

int Total_Number_of_Seconds;

int Hours=Total_Number_of_Seconds/3600;

int Remainder=Total_Number_of_Seconds%3600;

int Minutes=Remainder/60;

int Seconds=Remainder%60;

if (Total_Number_of_Seconds<50000);

cout << "What is Total_Number_of_Seconds:\n";

cin >> Total_Number_of_Seconds;

cout << "What is Hours:\n";

cin >> Total_Number_of_Seconds/3600;

cout << "What is Remainder:\n";

cin >> Total_Number_of_Seconds%3600;

cout << "What is Minutes:\n";

cin >> Remainder/60;

cout << "What is Seconds:\n";

cin >> Remainder%60;

if (Total_Number_of_Seconds>50000);

cout << "ERROR:" << endl;

return 0;

}

[/CODE]

You cannot use std::cin to compute a value.

#include <iostream>

using namespace std;

int main()

{

int Total_Number_of_Seconds;

cout << "What is the total number of seconds? ";

cin >> Total_Number_of_Seconds;

if( Total_Number_of_Seconds > 50000 )

{

cout << "error in input\n" ;

}

else

{

int Hours = Total_Number_of_Seconds / 3600;

int Remainder = Total_Number_of_Seconds % 3600;

int Minutes = Remainder / 60;

int Seconds = Remainder % 60;

cout << "The number of hours is: " << Hours << '\n' ;

cout << "The number of minutes is: " << Minutes << '\n' ;

cout << "And the number of seconds is: " << Seconds << '\n' ;

}

}

- Add to this Answer
- Ask a Question

Rating(1-10) | Knowledgeability = 10 | Clarity of Response = 10 | Politeness = 10 |

Comment | The code works fine, thank you very much. |

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

about 15 years or so**Education/Credentials**

post graduate engineer