Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
int elapsedtime(int time1, int time2)
{
int minutes1 = ((time1/100*60)+(time1%100);
int minutes2 = ((time2/100*60)+(time2%100);
return(minutes2-minutes1);
}
/**************************************************************************************
do
{
cout << "What is your starting time? ";
cin >> time1;
cout << "What is your ending time? ";
cin >> time2;
if (time2>=time1)
{
cout << "Your elapsed time is " << elapsedtime <<endl;
cout << "If you would like to enter other times?(Y/N)" <<endl;
cin >> choice;
}
else
{
cout << "Error: Your starting time must be lower than your ending time."<<endl;
cout << "If you would like to enter other times?(Y/N)" <<endl;
cin >> choice;
}
}
I keep getting an output of 1 for every elapsed time?
You have
cout << "Your elapsed time is " << elapsedtime <<endl;
Did you mean
cout << "Your elapsed time is " << elapsedtime(time1, time2) <<endl;
Update
For some reason,
cout << "Your elapsed time is " << elapsedtime <<endl;
does not produce a compiler error under g++ 4.7.3 even though the intent is to use elapsedtime(time1, time2).
I was able to successfully compile and build:
#include <iostream>
using namespace std;
int elapsedtime(int time1, int time2)
{
return 0;
}
int main()
{
cout << "Your elapsed time is " << elapsedtime <<endl;
}
Perhaps another SO post is needed to answer the question, "How does g++ not report error with the above code?".
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
So, I have recently started learning C++ (I am in first year college). I have been stuck with this error for a while now and none of my peers have been able to assist me in fixing this issue.
The error I am receiving is error: expected '{' before else
Here is the code:
switch (selectOption)
{
case 1:
cout << "Enter amount of people" << endl;
cin >> amountPpl;
//validating that there is more than 0 people
if (amountPpl > 0)
{
total = amountPpl * 30;
cout << "Your total for " << amountPpl << " is R" << total << endl;
}
else
{
cout << "You can't enter 0 people" << endl;
}
break ;
case 2:
cout << "Enter amount of Students" << endl;
cin >> amountPpl;
//validating input
if (amountPpl > 0)
{
total = amountPpl * 25;
cout << "The amount for " << amountPpl << "students is R" << total << endl;
}
else
{
cout << "You can't enter 0 people" << endl;
}
break;
case 3:
cout << "Are There any additional children? Answer with y/n" << endl;
cin >> decision;
//validating input
if (decision == 'y')
{
cout << "Please enter amount of additional children" << endl;
cin >> extraKids;
if (extraKids > 0)
{
total = 75 + (extraKids * 15 );
cout << "Your total is R" << total << "For the family package" << endl;
}
else
{
cout << "You cant enter 0 kids" << endl;
}
else // ***ERROR*** is here
{
cout << "Your total is R75";
}
}
break;
default:
cout << "invalid input, you can only select a number from 1-3 ";
}
link to an image
The error is in this section:
if (extraKids > 0)
{
total = 75 + (extraKids * 15 );
cout << "Your total is R" << total << "For the family package" << endl;
}
else
{
cout << "You cant enter 0 kids" << endl;
}
else // ***ERROR*** is here
{
cout << "Your total is R75";
}
#MikeCAT is correct. You cannot have two else sections. The first else should become an else if. The condition is obvious from the output string.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
double const A = 0.80,
B = 0.60,
C = 0.40,
D = 0.20;
char code1,
code2;
int volume,
student;
double price,
totalcost,
totalvolume,
totalprofit;
cout << "Enter the number price for one book :";
cin >> price;
cout << "Enter the book in stock :";
cin >> volume;
cout << "Enter the number of student :";
cin >> student;
cout << "Is the book required or suggested? Enter 'R' or 'S' :";
cin >> code1;
cout << "Is the book new or used? Enter 'N' or 'U' :";
cin >> code2;
if (code1 == 'R' && code2 == 'N')
{
totalvolume = student * A - volume;
cout << " We need to buy :" << totalvolume << endl;
if (totalvolume !=0)
totalcost = totalvolume * price;
cout << " totalcost is :" << totalcost << endl;
}
else
{
}
cout << " We need to buy :" << totalvolume << endl;
return 0;
}
I wonder if anyone can help me with this, is there a way I can ignore the second output message("totalcost is") and only print out the("We need to buy") when the totalvolume is 0? This is my first project in college, while loop haven't been taught yet and not allowed to use. I can only use if statement or switch. Thank you.
This snippet of code:
if (totalvolume !=0)
totalcost = totalvolume * price;
cout << " totalcost is :" << totalcost << endl;
The third line in this code snippet "totalcost is: " will always print if the outer "if" statement condition is satisfied. If you want it to print only when this if statement is true, you have to enclose it in curly braces {}. So it should look like this:
if (totalvolume !=0){
totalcost = totalvolume * price;
cout << " totalcost is :" << totalcost << endl;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
My program won't run I get an error message saying:
`error: no match for 'operator<<' (operand types are 'std::string' {aka 'std::__cxx11::basic_string<char>'} and '<unresolved overloaded function type>')|`
On the cout << address, " ", street << endl; line
I was using VS2017 but switched to CodeBlocks mid-way through
I have Windows 10 Pro Ryzen 5 2400G, 1060 6gb 16gb ram
Here is my program:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name, city, state, road, country, street;
int address;
cout << "Enter your: Name\n";
cin >> name;
cout << "Enter your Street\n";
cin >> street;
cout << "Enter your: Address\n";
cin >> address;
cout << "Enter your:\n City\n";
cin >> city;
cout << "Enter your: Province/State\n";
cin >> state;
cout << "Enter your: Country\n";
cin >> country;
//Output
cout << name << endl;
cout << address, " ", street << endl;
cout << city, " ", province, " ", country;
}
Thanks in advance!
Your syntax is wrong. You can't use , to chain arguments to cout like that. Instead do:
cout << address << " " << street << endl;
cout << city << " " << province << " " << country;
Your last two statements are syntactically incorrect. They should be as follows -
cout << address<<" "<<street << endl;
cout << city<< " "<< province<<" "<< country;
You probably are trying to use something like python in C++. But obviously that doesn't work. Each time you just have to keep doing cout<< variable1 << " " << variable2<< " "; . This is how chaining works in C++. No shorthand to that
I hope this solves your issue!
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am making a basic vending machine without object orientation and need a menu system for some reason even when i enter 2 into menuChoice the first if condition is met and it prints 2Here's your choco bar sir" when i want a Museli bar!!
#include <iostream>
#include <string>
using namespace std;
int main()
{
int Choclate=0;
int Museli=0;
int CheesePuffs;
int Apple;
int Popcorn;
int menuChoice = 0;
while (menuChoice != -1)
{
cout << "-ENTER CORRESPONDING NUMBER-" << endl;
cout << "1. Milk Choclate Bar" << endl;
cout << "2. Museli Bar" << endl;
cout << "3. Cheese Puffs" << endl;
cout << "4. Apple" << endl;
cout << "5. Popcorn" << endl;
cout << "Enter Choice: ";
cin >> menuChoice;
if (menuChoice = 1)
{
Choclate = Choclate + 1;
cout << "Here's your choco bar sir." << endl;
}
else if (menuChoice = 2)
{
Museli = Museli + 1;
cout << "Here's your museli bar sir." << endl;
}
}
}
You need to use ==
i.e.
if (menuChoice == 1)
Better still look up switch
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am having issues with converting age in years to seconds. It comes up with a wrong number and a negative one. Can someone please help?
#include <iostream>
using namespace std;
int main()
{
int years;
years = years;
cout << "How many years have you lived: " ;
cin >> years;
cout << "You have lived: " << years; cout << " years:" << endl;
int days = 0.5 + (years * 365.25);
cout << days; cout << " days" << endl;
int hours = days * 24;
cout << hours; cout << " hours" << endl;
int minutes = hours * 60;
cout << minutes; cout << " minutes" << endl;
int seconds = minutes * 60;
cout << seconds; cout << " seconds" << endl;
return 0;
}
You are getting wrong/negative seconds because of overflow problem. int can't hold so big numbers (seconds).
So, use long long minutes and long long seconds instead of int
And, don't forget to remove the line
years = years;