"0" From nowhere after cout [closed] - c++

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
When i compile and run the program i am getting:The year you were born: 0
Where is the "0" coming from??
Here is the code:
//! Program written by Samer!//
#include <iostream>
using namespace std;
int main()
{
double Year, Age;
cout <<"The year you were born: "<< Year; //!Here the error appears!//
cin >>Year;
while (Year > 2017) //!That't a While loop!//
{
cout <<"Please enter a valid Year:" << Year << endl;
cin >>Year;
}
Age=2017-Year;
cout <<"Your age is:" <<Age;
std::cin.get();
return 0;
}

You are streaming the variable Year:
cout <<"The year you were born: "<< Year;
^^^^
Your code is by design printing Year. This is what you have asked it to do. If you don't want to print it, then don't: cout <<"The year you were born: \n";
Year has not been initialized and using it in this way is undefined behaviour. In this case it seems to print whatever was in the memory at the time of initialization. In your case it happens to print 0.

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

only last line gets saved in text file in c++ [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 7 years ago.
Improve this question
my code only saves the last line for eg if i enter 1 abc then press enter and then type 2 def then only 2 def is saved in txt file.
here is my code :-
int main()
{
ofstream rankings;
rankings.open("rankings.txt");
cout << "Enter rank of the Student <space> followed by Name\n"
"Press Ctrl+Z to quit"<< endl;
int rank;
string name;
while (cin >> rank >> name);
{
rankings << rank << ' ' << name << endl;
}
rankings.close();
return 0;
}
You have a superfluous semicolon after your while loop:
while (cin >> rank >> name);
// ^
This will just open a new block in the code afterwards, and leave you with the least values input.
To fix change your loop to
while (cin >> rank >> name) {
rankings << rank << ' ' << name << endl;
}

C++ do while loop [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 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);