gpt4 book ai didi

c++ - 字符串变量的简单错误

转载 作者:行者123 更新时间:2023-11-30 01:49:57 24 4
gpt4 key购买 nike

好吧,我是 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";

}

最佳答案

在 C 语言中,您用单引号 ('c') 括起来的所有内容都被视为一个字符。如果要使字符串使用双引号:"John Smith"

关于c++ - 字符串变量的简单错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28196098/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com