Reputation: 3
Ok so I am a beginner to C++, using Visual Express C++ 2010 as my compiler. I have an assignment to create a program that calculates the net pay and budget of John Smith. I should be able to use the string variable 'John Smith', I used #include . I'm not sure why it says that I exceed the character count, because I should be using a string, not a variable. Sorry for the excessive comments, they're required for the assignment. And I'm sure that this is a very basic code format, but as I said, I'm new. Thanks in advance
// Module 2 Programming Challenge Right.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
// This program should calculate the base pay and budget of John Smith
#include <iostream>
#include <string>
using namespace std;
int main()
{
int BasePay; // I am Ben Chilton, this is the Module 2 programming challenge, this was started on 1/26/2015
int Commission; // The purpose of this program is to calculate the gross pay, deductions, net pay, and sales value
int GrossPay; // of the employee John Smith. It also calculates his budget by using his net pay
int Deductions;
int NetPay;
int Sales;
int HousingBudget;
int FACBudget; // Food and clothes budget
int EntBudget; // Entertainment budget
int MiscBudget; // Miscellaneous budget
string JohnSmith; //string should have a use? It's not highlighted blue, even though I used " #include <string> "
// I'm using a string variable here, not character, not sure what the problem is
BasePay = 900;
Sales = 3500;
Commission = (Sales * 6) / 100; // He earns 6% commission on sales, hence multiplying by 6 and dividing by 100
GrossPay = Commission + BasePay; // Gross pay is his base pay plus his commission on sales
Deductions = (GrossPay * 18) / 100; // Again I used multiplication and division to calculate percentages
NetPay = GrossPay - Deductions; // His net pay is his gross pay minus his deductions, done here
HousingBudget = (NetPay * 30) / 100; //
FACBudget = (NetPay * 15) / 100;
EntBudget = (NetPay * 50) / 100; // Interesting budget choices haha
MiscBudget = (NetPay * 5) / 100;
JohnSmith = 'John Smith'; // Not sure why it is identifying this as a character, not a string
cout << "Hello " << JohnSmith << " . We'll be calculating your income and budget. \n";
cout << "Your base pay is " << BasePay << " dollars. \n";
cout << "You made " << Sales << " dollars in sales. \n";
cout << "You earned " << Commission << " dollars in commission from your sales. \n";
cout << "Your gross pay is " << GrossPay << " dollars . \n";
cout << "Your deductions are " << Deductions << " dollars. \n";
cout << "Your net pay is " << NetPay << " dollars. \n";
cout << "Your housing budget is " << HousingBudget << " dollars. \n";
cout << "You have " << FACBudget << " to spend on food and clothes. \n";
cout << "Your budget for entertainment is " << EntBudget << " dollars. \n";
cout << "That leaves you with " << MiscBudget << " dollars to spend on anything else. \n";
}
Upvotes: 1
Views: 313
Reputation: 642
This is because you are using wrong syntax for assigning the string. A string is assigned like string var= "John Smith" . The syntax you are using is for assigning the character. Like if you want to assign a char to a var then it would be like char ch = 'A'. I hope it would help.
Upvotes: 0
Reputation: 65770
The compiler thinks that 'John Smith'
should be a character because you are using single quotes instead of double quotes. In c++, 'x'
means a char
, whereas "x"
means a const char[2]
, that is a string literal. To use strings, use "John Smith"
.
Upvotes: 2
Reputation: 4435
In C everything you embrace in single quotes ('c'
) is treated as a character. If you want to make a string use double quotes: "John Smith"
.
Upvotes: 2