gpt4 book ai didi

c++ - 在 C++ 的条件语句中使用字符

转载 作者:行者123 更新时间:2023-11-28 03:31:07 25 4
gpt4 key购买 nike

我正在尝试编写一个程序,要求获得学费学分以及本科或研究生类(class)。用户输入学分数,然后必须为本科输入 U 或为研究生输入 G。我的条件语句有问题,如果用户输入 U,那么本科学分的价格将计算并输出,与研究生类似。我试图在 IF 条件下输入 U,但要么是一个价格,要么是其他输出。

#include <stdlib.h>                             
#include <iostream.h>

int main ()
{
const double Under = 380.00 ;
const double Grad = 395.00 ;
char U , G ;
double First, Second, Third, Fourth, Fifth, Sixth ;

cout << endl << endl ;
cout << " To calculate your tuition enter the amount of credits, then enter type of" ;
cout << endl ;
cout << " classes." ;

cout << endl << endl ;
cout << " Enter number of credits. " ;
cin >> First ;
cout << endl << endl ;

cout << " Enter U for Undergraduate or G for Graduate: " ;
cin >> Second ;

cout << endl << endl ;
cout << " Your tuition total is: " ;

Third = First * Under ;

Fourth = First * Grad ;

if ( Second == U )
cout << Third ;

else
cout << Fourth ;
cout << endl << endl ;

system ("Pause");

}

最佳答案

好的,我在这里看到了一些问题。

最主要的是C++中的字符有单引号,像这样'c' .这很可能是您出错的原因。由于您从未在任何地方初始化 U,因此请将其初始化为 'U'或者试试

if ( Second == 'U' )
cout << Third ;

其次,尽管这不一定是输入 cout<<endl<<endl; 的错误有点浪费,因为它两次刷新 cout 的缓冲区,中间只添加了 1 个字符。打字cout<<'\n'<<endl;会解决这个问题。

关于c++ - 在 C++ 的条件语句中使用字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12613714/

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