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;
}
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.
Related
So I perused some of the other articles, but I can't seem to find a reason to why this won't work. I am new to C++ so be kind please.
// User Pay.cpp : Defines the entry point for the console application.
// This program calculates the user's pay.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
double hours, rate, pay;
// Get the number of hours worked.
cout << "how many hours did you work? ";
cin >> hours;
// Get the hourly pay rate.
cout << "How much do you get paid per hour? ";
cin >> rate;
//C alculates the Pay.
pay = hours * rate;
// Display the pay.
cout << "You have earned $" << pay << endl;
return 0;
}
You dont need to include #include "stdafx.h".
Also a better practice for the future is not to include the whole std library ("using namespace std"). Instead of this you can call directly std::cout, std::cin etc...
Also a system("PAUSE") call at the end of the code before "return 0" would be helpful (in your example). So the console doesn't close when the program execute and you can see your result.
Code example:
#include <iostream>
//using namespace std;
int main()
{
double hours, rate, pay;
// Get the number of hours worked.
std::cout << "how many hours did you work? ";
std::cin >> hours;
// Get the hourly pay rate.
std::cout << "How much do you get paid per hour? ";
std::cin >> rate;
//C alculates the Pay.
pay = hours * rate;
// Display the pay.
std::cout << "You have earned $" << pay << std::endl;
system("PAUSE");
return 0;
}
Try creating an empty project (uncheck precompiled headers). then copy your code but delete #include "stdafx.h".
It looks as if you had an error, and then added:
`using namespace std;`
There should be no error now.
I've been wondering how to go about running a program within another program where the the main process can output into the stdin of the secondary process as well as have that second process output to the input to the primary one.
The closest I have come to finding a term for this idea is pipes and forks but I don't quite get the examples i've seen. I've only seen ones where the same program is using pipes to launch itself again. Additionally all of these examples assume that that the programmer is writing/has access to both program's source code.
I would like to be able to interface in this way with programs that are already compiled. Here's an example of what I would like to be able to do:
This is the "compiled" program-
#include <iostream>;
using namespace std;
int main(){
int answer = 42;
int guess = 0;
do{
cout << "Guess a number..." << endl;
cin >> guess;
if(guess<answer)
cout << "Guess Higher!" << endl;
else if(guess>answer)
cout << "Guess Lower!" << endl;
}while(answer!=guess);
cout << "You win!";
return 0;
}
This is the "parent" program-
#include <iostream>;
using namespace std;
int main(){
//Code executing and connecting the other program
//while cin/cout would be nice for keeping it clean, other methods of doing this are fine
//I used cin/cout as placeholders to try and make what I am asking clearer
String out;
cin >> out;//Load initial prompt
int high=100, low=0;
do{
cout << (high+mid)/2 << endl;
cin >> out;//Load response
if(out.compare("Guess Higher!"))
low = (high+low)/2;
else
high = (high+low)/2;
}while(out.compare("You win!")!=0);
return 0;
}
The idea here is that my "parent" program could play this game for me. It would make a guess, view the response, use some logic to decide what to do next, guess, repeat until it wins. This particular example is pretty useless but the same general idea of controlling one program with another has a lot of uses. This is just a hypothetical demo of would I would like to be able to do.
Thanks to anyone kind enough to take time out of their day to help.
I'm new to c++ I'm trying to do a simple programme that calculates the weekly pay. See source code below. When I run the programme it lets me enter the workHours but when I click enter the programme closes n does not proceed to the rest of the code. Not sure where I'm going wrong please help.
#include <iostream>
using namespace std;
int main ()
{
int workDays;
float workHours, payRate, weeklyPay;
workDays = 5;
payRate = 38.55;
cout<< "Enter the number of hours worked: ";
cin >> workHours;
cin.ignore ();
weeklyPay = workDays * workHours * payRate;
cout << "Weekly Pay = ";
cout << weeklyPay;
cout << '\n';
return 0;
}
The DevC++ doesn't pause the program after it ends you have to add this feature by yourself:
First include conio.h
#include <conio.h>
Then at the bottom of the main add getch()
getch();
What compiler you are using as i am to run this code?
I am using visual studio 10 as IDE and it is working for me.
I am usually a Java programmer, and have used textmate for that almost exclusively, but lately I started using C++ with it. but when i use even the most basic programs and incorporate the cin keyword, and run the program, I dont get an oppurtunity to put in anything during runtime and sometimes it inserts random values by itself! for example, if i ran this in textmate:
#include <iostream>
int stonetolb(int);
int main() {
using namespace std;
int stone;
cout << "enter the weight in stone";
cin >> stone;
int pounds = stonetolb(stone);
cout << stone << "stone = ";
cout << pounds <<" pounds.";
return 0;
}
int stonetolb(int sts) {
return 14 * sts;
}
I would come out with the output:
enter the weight in stone32767stone = 458738 pounds.
Why is this happening, and how do I stop it?
Most likely, the input statement cin >> stone is failing, and stone has an undefined value. You need to check for input failure by using if (cin >> stone) { ... } else { // input failure }. As for why such a simple program would exhibit failing behaviour, I don't know- you would have to check the textmate documentation.
For some reason, this very basic code will compile with no errors in Visual C++, but gives errors in XCode. I will need to know why, in order to continue working in Xcode for my Computer Science class.
#include <iostream>
#include <string>
using namespace std;
struct acct { // bank account data
int num; // account number
string name; // owner of account
float balance; // balance in account
};
int main() {
acct account;
cout << "Enter new account data: " << endl;
cout << "Account number: ";
cin >> account.num;
cout << "Account name: ";
cin >> account.name;
cout << "Account balance: ";
cin >> account.balance;
return 0;
}
It gives two errors, one saying that it expected ';' before account (after main is declared), and the second that account was not declared for cin >> account.num;
The problem is not actually in your code: while C does require you to prefix your variables with struct in this case, C++ does not. The problem is actually that there is a global function on Unix named acct - it is this that is confusing the compiler. If you renamed your struct to something else, say bank_account, it will behave as you expected.
If you change the "acct account;" in main to "struct acct account;" it should compile. You haven't actually declared a new typedef "acct" in your code, but Visual C++ helpfully does one for you as a non-standard extension. XCode is more strict.
An alternative fix is to do "typedef struct acct { ... } acct;" which will both declare the acct structure and create a new typedef.
I've run into similar problems trying to use a variable named "log."
If you want to keep your structure name, try specifying just the elements you want to use:
using std::cin;
using std::cout;
using std::endl;