Reputation: 1
Ive been following along with some c++ tutorials on youtube and I cant seem to get this code to work, I use code blocks and this person uses visual c++. Is there a difference between them somehow? before I did cin >> std::Asalary; she asked what whould happen if you ran this code, she got an error and I got zero which makes sense, so im kinda lost on both of these problems. any help whould be epic, thanks in advance.
#include <iostream>
using std::cout;
using std::endl;
using std::string;
namespace main1 {
double Asalary;
double MonthSal = Asalary/12;
}
int main()
{
cout << "enter your annual salary" << endl;
cin >> main1::Asalary;
cout << main1::MonthSal << endl;
}
Upvotes: 0
Views: 51
Reputation: 11440
You have at global scope:
namespace main1
{
double Asalary;
double MonthSal = Asalary/12;
}
and then you do:
cin >> main1::Asalary;
cout << main1::MonthSal << endl;
So, you seem to expect main1::MonthSal
to magically be annual salary divided by twelve, because you told the program once.
That's not how C++ works. double MonthSal = Asalary/12;
is executed only once, before annual salary is entered.
Then, if you change annual salary, the monthly salary will not update.
I know that's not the question you are asking, but this is important and will hinder your understanding of C++ in a significant way.
Upvotes: 3