C++ do while loop [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I tried to make a program to get the amount after certain days of investment at some rate.my try was the below program it didnt gave any errors but also it didnt gave the result.please help.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float a;
float p;
float r ;
int days;
int day;
cout << "Enter the a principal amount"<<endl;
cin >>p;
cout <<"Enter the rate "<<endl;
cin >>r;
cout << "Enter number of days"<<endl;
cin>> days;
do(){
a= p* pow(1+r,day);
cout << day << "-------"<<a<<endl;
day++;
}
while (day <=days);
}`

1.You haven't initialized 'day' variable.
----> day=1;
2.The syntax of do while is wrong.
do{
}while(condition);

Related

error: no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'void')| [closed]

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
This is my code. I have gone through it multiple times, making many changes still same error.
#include <iostream>
using namespace std;
void checkAge(int age){
if(age >= 18){
cout<< "As your age is above 18, you are eligible to vote. \n";
}
else{
cout<< "As your age is below 18, you aren't eligible to vote. \n";
}
}
int main()
{
int age;
cout << "Enter your age. \n";
cin >> age;
cout << checkAge(age);
return 0;
}
Your function checkAge does not return anything. So just remove the cout from
cout << checkAge(age);
That is replace the above statement to just:
checkAge(age);
Solution 2
Another solution would be to return an int from the checkAge. For example you could change you function definition to:
#include <iostream>
using namespace std;
void checkAge(int age){
if(age >= 18){
cout<< "As your age is above 18, you are eligible to vote. \n";
}
else{
cout<< "As your age is below 18, you aren't eligible to vote. \n";
}
return age;//added return so that cout << checkAge(age) would work
}
int main()
{
int age;
cout << "Enter your age. \n";
cin >> age;
cout << checkAge(age);
return 0;
}

ā€˜iā€™ was not declared in this scope for (i = 0; i <= years; i++) [closed]

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 4 years ago.
Improve this question
For some reason, myprogramminglab keeps saying that I have not declared what 'i' and I don't understand why.
#include <iostream>
using namespace std;
int main()
{
int years;
double cost, inflaRate;
cout<<"Enter the current price of pencils:";
cin >> cost;
cout<<"Enter the number of years in the future that
you will buy the pencil:";
cin >> years;
cout<<"Enter the inflation rate as a percentage." <<
endl;
cin >> inflaRate;
inflaRate /= 1;
cout << "The price of pencils will be " << cost;
for (i = 0; i <= years; i++) //Keeps telling me I have not declared 'i' here
{
cost += (cost*inflaRate);
}
cout << cost << "in" << years << "years." << endl;
system("pause");
return 0;
}
For some reason ...
That reason is because you haven't actually declared i. You can fix that with a simple change to your for loop:
for (int i = 0; i <= years; i++) // No longer complains you have not declared 'i' :-)
// ^^^
// Declare it!

if statement facing error in percentage C++ code [closed]

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 4 years ago.
Improve this question
I have faced the problem the formula of percentage doesn't work properly
#include <iostream>
using namespace std;
int main() {
int tmarks,intermarks, passmarks;
float per;
cout << "Enter Your Inter Marks:\n";
cin >> intermarks;
cout << "Enter Your Total Marks:\n";
cin >> tmarks;
cout << "Enter Your PassMarks:\n";
cin >> passmarks;
per = (intermarks/tmarks) * 100;
cout << "percentage:" << per;
if (per >= 45 && passmarks >= 50) {
cout << "Welcome To Uni\n";
} else {
cout << "Improve Your Marks You are eligible\n";
}
}
If intermarks = 50 and tmarks = 75, then intermarks/tmarks will be 0. Since both are integers. You need to typecast before division operation. This way float(intermarks) / float(tmarks) will be 0.67 and per will be 67

Why the result differs when I press enter at the end of a sentence? [closed]

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 6 years ago.
Improve this question
Here is my first program
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a;
string s;
double d;
while(cin >> a >> s >> d)
cout << a << s << d;
return 0;
}
When I input some simple data and press Enter , the result is shown immediately:
However, the code in another program behaves differently:
#include <iostream>
#include <string>
using namespace std;
struct Sales_data {
string bookNo;
unsigned units_sold = 0;
double price = 0.0;
void Print();
};
void Sales_data::Print(){//print every record of the Sales_data
cout << "The bookNo of the book is " << bookNo << endl;
cout << "The units_sold of the book is " << units_sold << endl;
cout << "The price of the book is " << price << endl;
}
int main()
{
Sales_data book;
while(cin >> book.bookNo >> book.units_sold >> book.price);
book.Print();
return 0;
}
When I run this code, input some data, and press Enter, it waits for me to input more data rather than show the result.
Could you explain this to me?
Remove the semicolon after the while loop. As it is, it forces the loop to have no body, which means it just cycles over the cin forever. Even better, use braces to delimit the body:
while(cin >> book.bookNo >> book.units_sold >> book.price) {
book.Print();
}

Make a C++ program ask something many times [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Can someone help me? I want to make a program that asks me "Whats the first?", "Whats the second?", "Whats the third?" etc. I want it to ask me that as many times as I have told it. It is stored in the int noftimes.
#include <iostream>
#include <string>
using namespace std;
int main()
{
//Get number of grades.
int nofgrades = 7;
cout << "How many grades do you want to calculate?\n";
cin >> nofgrades;
//Get grades.
cout << "What's the " << (nofgrades - (nofgrades - 1)) << "st grade?\n";
//Calculate average.
//Display average.
return 0;
}
I'm programming in C++. All help is appreciated.
Here you go, by using for loop it's simple to do what you ask. Read a bit more about for loops and general programming principles. Look at this subreddit sidebar for good resources.
#include <iostream>
#include <string>
using namespace std;
int main()
{
//Get number of grades.
int nofgrades; // Why did you initialize it when you're going to override it anyway?
cout << "How many grades do you want to calculate?\n";
cin >> nofgrades;
//Get grades.
for (int i = 1; i <= nofgrades; ++i) {
cout << "What's the " << i << "st grade?\n";
//Load aditional grades...
}
//Calculate average.
//Display average.
return 0;
}