字符串变量的简单错误

时间:2015-01-28 15:29:55

标签: c++ string

好的,所以我是C ++的初学者,使用Visual Express C ++ 2010作为我的编译器。我有一项任务是创建一个计算John Smith的净工资和预算的程序。我应该能够使用字符串变量' John Smith',我使用#include。我不确定为什么它说我超过字符数,因为我应该使用字符串,而不是变量。对于过多的评论感到抱歉,他们需要完成作业。而且我确信这是一种非常基本的代码格式,但正如我所说,我是新的。提前致谢



// 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";

}
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

在C中,您用单引号('c')包含的所有内容都被视为一个字符。如果您想创建一个字符串,请使用双引号:"John Smith"

答案 1 :(得分:2)

编译器认为'John Smith'应该是一个字符,因为您使用的是单引号而不是双引号。在c ++中,'x'表示char,而"x"表示const char[2],即字符串文字。要使用字符串,请使用"John Smith"

答案 2 :(得分:0)

这是因为您使用了错误的语法来分配字符串。字符串的分配类似于 string var =“John Smith”。您使用的语法是分配字符。就像你想为一个变量指定一个char一样,那就像 char ch ='A'。      我希望它会有所帮助。