Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So, here is the updated thread: Maybe there is white space in there that I'm not seeing? It is the exact same error as it was before. You anyone can think of anything to try, ill do it.
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
// 1. Initialize Objects
double retail = 0.0;
double markup = 0.0;
double wholesale = 0.00;
// 2. Input
cout << "Hello Emily CO 127: ";
cout << "Enter Retail Price: ";
cin >> retail;
cout << "Enter Mark Up Percent: ";
cin >> markup;
// 3. Process
wholesale = retail * (1 + markup);
cout << " Wholesale " << wholesale << endl;
return 0;
system("pause");
}
using namespace std; is a declaration that introduces the identifiers from the namespace called std in the global scope. It is not the beginning of function or starting point for a block. What you're missing is your main function:
int main() // start of the program
{
// ...
}
This is what your program should look like then:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
// 1. Initialize Objects
double retail = 0.0;
double markup = 0.0;
double wholesale = 0.00;
// 2. Input
cout << "Hello Emily CO 127: ";
cout << "Enter Retail Price: ";
cin >> retail;
cout << "Enter Mark Up Percent: ";
cin >> markup;
// 3. Process
wholesale = retail * (1 + markup);
cout << " Wholesale " << wholesale << endl;
return 0;
system("pause");
}
You should read up more on the basics of C++, they will explain everything you need to know.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So basically, I'm new to programming and can't figure it out how to add certain numbers generated by the user. I want a program, capable of collecting for n days the temperature, and at the end, inform what the peak heat occurred, as well as the average of the temperatures collected.
#include <iostream>
using namespace std;
int main()
{
int days, day0 = 0, degrees, degrees_max = 0;
float degrees_average;
cout << "Days of the study" << endl;
cin >> days;
while (days > day0)
{
cout << "How many Degrees?" << endl;
cin >> degrees;
if (degrees < 999)
{
day0++;
}
if (degrees > degrees_max)
{
degrees_max = degrees;
}
}
degrees_average = degrees / days;
cout << "The max temperature was: " << degrees_max << "C"
<< " And the average was : " << degrees_average << endl;
}
SO the only thing left is how to calculate the average.
All the help is appreciated! Thanks
#include <iostream>
using namespace std;
int main()
{
int days, max=-1;
double current, total = 0;
cin >> days;
for (int i=0; i<days; i++){
cin >> current;
if (current > max) max = current;
total = total + current;
}
cout << "max: " << max << endl;
cout << "average: " << total/days << endl;
return 0;
}
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 5 years ago.
Improve this question
I'm making a mortgage calculator for my class, the program runs but the answer isn't correct, I think I have the right formula but maybe I'm declaring my variables wrong.
This is what I have so far
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
//Variables
double rate;
rate = rate / 1200.0;
double term;
term = term * 12;
double principal;
double monthlyPayment;
monthlyPayment = (principal * rate) / (1.0 - pow(rate + 1, -term));
//Gathering Values
cout << "Please enter your interest rate." << endl;
cin >> rate;
cout << "Please enter your term." << endl;
cin >> term;
cout << "please enter your principal" << endl;
cin >> principal;
//Formula
monthlyPayment = principal * rate / (1.0 - pow(rate + 1, -term));
//Result
cout << "Your monthly payment is, " << monthlyPayment << endl;
return 0;
}
All of the math you do before the cin statements is not being factored in to your calculation. Also, your second formula calculating monthlyPayment is missing parentheses in the numerator.
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
Basically I want to square any number I input. Why does this not work?
I compiles but it does not square my input.
#include <iostream>
using namespace std;
float SquareNumber;
float Squared = SquareNumber * SquareNumber;
int main()
{
cout << "Please enter a number to be squared: ";
cin >> SquareNumber;
cout << SquareNumber << " squared is " << Squared;
system("pause");
return 0;
};
I compiles but it does not square my input.
You need to compute the square after you input the number. At the time Squared is computed, the value of SquaredNumber is 0.0. Hence, the value of Squared is also 0.0.
The line
float Squared = SquareNumber * SquareNumber;
sets the value of Squared using the value of SquaredNumber at that time. It does not update the value of Square when the value of SquaredNumber is changed. To get that effect, you need to use a function.
#include <iostream>
using namespace std;
float SquareNumber;
float Squared(float in)
{
return in * in;
}
int main()
{
cout << "Please enter a number to be squared: ";
cin >> SquareNumber;
cout << SquareNumber << " squared is " << Squared(SquareNumber);
return 0;
};
Your problem is that you are not correctly calculating the square. You do it as "compile time", rather than at "run time".
Just change your code to this, but be sure that you understand why - and ask here if you do not (hint: how can you calculate the square of a number, before you know what the number is?).
#include <iostream>
using namespace std;
int main()
{
float SquareNumber;
float Squared;
cout << "Please enter a number to be squared: ";
cin >> SquareNumber;
Squared = SquareNumber * SquareNumber; // calculate at *run time*
cout << SquareNumber << " squared is " << Squared;
system("pause");
return 0;
};
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Basically I have finished programming a little times tables quiz for my little brother.I am very new to programming and I have no idea how to reset my program back to the start. Can anyone shed some light on this?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int sum ;
int question = 0;
int Uanswer ;
int score = 0;
int *score_pointer = &score;
//============================================================//
cout<<"= Hello Welcome to Your Times Tables ="<<endl;
cout<<"= Please Select A Table To Learn Below ="<<endl;
cout<<"========================================"<<endl;
cout<<"####################################"<<endl; /* THE MENU */
cout<<"####|1|2|3|4|5|6|7|8|9|10|11|12|####"<<endl;
cout<<"####################################"<<endl;
//============================================================//
cout<<">>:";
cin>> sum;
cout<<"You Selected "<< sum <<endl; /*User Input For Table */
cout<<"Time to Learn Brain Power Go!"<<endl;
//============================================================//
while (question <12){
question = question +1;
cout<< question;
cout<< "x";
cout<< sum ;
cout<< "=:";
cin>> Uanswer;
int Aanswer = (sum * question);
if (Aanswer == Uanswer){
cout<<"Correct: Well done Ben :)"<<endl;
*score_pointer = *score_pointer+1;
} else {
cout<<"Incorrect: Are you even trying? :("<<endl;
}
}
//====================================//
cout<<"Well done You scored " << score << " Out of 12"<<endl; /*Tally of total score */
//====================================//
return 0;
}
Use a do while loop to enclose your code and set the condition to what you want.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int flag=1;
do
{
//your code that needs to be repeated
//when you need to exit set flag=0
} while(flag);
}
Here is your program in a while loop. I would also recommend not using endl and instead using \n so you dont have to flush the stream so much.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
bool done = false;
while (!done) {
int sum;
int question = 0;
int Uanswer;
int score = 0;
int *score_pointer = &score;
//============================================================//
cout << "= Hello Welcome to Your Times Tables =" << endl;
cout << "= Please Select A Table To Learn Below =" << endl;
cout << "========================================" << endl;
cout << "####################################" << endl; /* THE MENU */
cout << "####|1|2|3|4|5|6|7|8|9|10|11|12|####" << endl;
cout << "####################################" << endl;
//============================================================//
cout << ">>:";
cin >> sum;
cout << "You Selected " << sum << endl; /*User Input For Table */
cout << "Time to Learn Brain Power Go!" << endl;
//============================================================//
while (question < 12){
question = question + 1;
cout << question;
cout << "x";
cout << sum;
cout << "=:";
cin >> Uanswer;
int Aanswer = (sum * question);
if (Aanswer == Uanswer){
cout << "Correct: Well done Ben :)" << endl;
*score_pointer = *score_pointer + 1;
}
else {
cout << "Incorrect: Are you even trying? :(" << endl;
}
}
//====================================//
cout << "Well done You scored " << score << " Out of 12" << endl; /*Tally of total score */
//====================================//
cout << "\nWould you like to try again? (y/n)";
char answer;
cin >> answer;
tolower(answer);
if (answer == 'n') done = true;
}
return 0;
}
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
my while loop wont run on the conditions I set it.
The purpose of the program is to determine the amount of years it will take a deposited amount to mature using the deposit amount, interest rate and target amount.
The program just stops after the target amount has been entered, unless I change the while statement from <= to >= in which case it runs the loop but returns the number of years set at round 100's or 1000 etc...
#include <iostream>
#include <iomanip>
#include <string>
#include <math.h>
using namespace std;
int main()
{
//declare the variables
double rate,
balance = 0;
int deposit,
target,
years = 0;
cout << "****Lets make you some money!****" << endl << endl;
//input from the user
cout << "What is your deposit amount?: " << endl;
cin >> deposit;
cout << "What is your interest rate?: " << endl;
cin >> rate;
cout << "What is you target savings amount?: " << endl;
cin >> target;
rate = rate / 100;
while (balance <= target); //when i change this to balance >= target the 'while' runs but just returns years divisible by 100
{
// calculation
balance += deposit * pow((1 + rate), years);
//balance = balance*(1 + rate) + deposit; // alternate calculation
//years++;
//users savings target
cout << "You will reach your target savings amount in: " << balance << " years." << endl << endl << " That's not that long now is it?" << endl;
}
return 0;
}
Thanks in advance!
The problem is an unfortunate suffix:
while (balance <= target);
// ^
That is equivalently:
while (balance <= target) {
;
}
{
// calculation, which always runs exactly once
// regardless of what balance/target are
}
Just drop the semicolon.