C++ 未初始化的局部变量使用C++;
我已经将C++ 未初始化的局部变量使用C++;,c++,char,int,C++,Char,Int,我已经将chara、b、c、d和f重写为int,然后给它们赋值,我仍然得到错误 C4700 uninitialized local variable used. 我怎样才能解决这个问题?我想要StudentsGrade=a;如果条件为真,则返回 #include "stdafx.h" #include<iostream> using namespace std; int main() { char a; char b; char c;
char
a
、b
、c
、d
和f
重写为int
,然后给它们赋值,我仍然得到错误
C4700 uninitialized local variable used.
我怎样才能解决这个问题?我想要StudentsGrade=a
;如果条件为真,则返回
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
char a;
char b;
char c;
char d;
char f;
char students;
double totalpoints = 0;
char StudentsGrade;
cout << "Enter Student's name";
cin >> students;
if (totalpoints > '360' && totalpoints < '400')
StudentsGrade = a;
else if
(totalpoints > '320' && totalpoints < '359')
StudentsGrade = b;
else if
(totalpoints > '280' && totalpoints < '319')
StudentsGrade = c;
else if
(totalpoints > '240' && totalpoints < '279')
StudentsGrade = d;
else
(totalpoints < '240');
StudentsGrade = d;
return 0;
}
#包括“stdafx.h”
#包括
使用名称空间std;
int main()
{
字符a;
字符b;
字符c;
chard;
字符f;
学生;
双倍总积分=0;
char StudentsGrade;
学生;
如果(总分>360分&总分<400分)
学生等级=a;
否则如果
(总分>320分&总分>359分)
学生等级=b;
否则如果
(总分>280分&总分>319分)
学生等级=c;
否则如果
(总分>'240'和&totalpoints<'279')
学生等级=d;
其他的
(总分<'240');
学生等级=d;
返回0;
}
在代码中,a
是一个变量,而不是实际的字母a。试试这个:
char a = 'a'; // Now the variable a (which is of type char) contains the character a.
对于所有的char
s,a
到f
。这将初始化变量。您还可以完全删除变量,如下所示:
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
char students;
double totalpoints = 0;
char StudentsGrade;
cout << "Enter Student's name";
cin >> students;
if (totalpoints > 360 && totalpoints < 400)
StudentsGrade = 'a'; // <-- Notice here!
else if
(totalpoints > 320 && totalpoints < 359)
StudentsGrade = 'b'; // <-- And so on...
else if
(totalpoints > 280 && totalpoints < 319)
StudentsGrade = 'c';
else if
(totalpoints > 240 && totalpoints < 279)
StudentsGrade = 'd';
else
(totalpoints < 240);
StudentsGrade = 'd';
return 0;
}
#包括“stdafx.h”
#包括
使用名称空间std;
int main()
{
学生;
双倍总积分=0;
char StudentsGrade;
学生;
如果(总点>360和总点<400)
StudentsGrade='a';//320&&totalpoints<359)
StudentsGrade='b';//280&&totalpoints<319)
学生等级='c';
否则如果
(总分>240和总分<279)
学生等级='d';
其他的
(总分<240分);
学生等级='d';
返回0;
}
我想这就是你最初打算做的。最后,请注意(在条件中)数字周围不再有
'
。在代码中,a
是一个变量,而不是实际的字母a。试试这个:
char a = 'a'; // Now the variable a (which is of type char) contains the character a.
对于所有的char
s,a
到f
。这将初始化变量。您还可以完全删除变量,如下所示:
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
char students;
double totalpoints = 0;
char StudentsGrade;
cout << "Enter Student's name";
cin >> students;
if (totalpoints > 360 && totalpoints < 400)
StudentsGrade = 'a'; // <-- Notice here!
else if
(totalpoints > 320 && totalpoints < 359)
StudentsGrade = 'b'; // <-- And so on...
else if
(totalpoints > 280 && totalpoints < 319)
StudentsGrade = 'c';
else if
(totalpoints > 240 && totalpoints < 279)
StudentsGrade = 'd';
else
(totalpoints < 240);
StudentsGrade = 'd';
return 0;
}
#包括“stdafx.h”
#包括
使用名称空间std;
int main()
{
学生;
双倍总积分=0;
char StudentsGrade;
学生;
如果(总点>360和总点<400)
StudentsGrade='a';//320&&totalpoints<359)
StudentsGrade='b';//280&&totalpoints<319)
学生等级='c';
否则如果
(总分>240和总分<279)
学生等级='d';
其他的
(总分<240分);
学生等级='d';
返回0;
}
我想这就是你最初打算做的。最后,请注意(在条件中)数字周围没有更多的
'
。您只需要做以下更改,使用实际值初始化char变量,如char a='a'
char a = 'a';
char b = 'b';
char c = 'c';
char d = 'd';
char f = 'f';
还有一些错误,您正在使用d两次。您只需要做以下更改,用实际值初始化char变量,如char a='a'
char a = 'a';
char b = 'b';
char c = 'c';
char d = 'd';
char f = 'f';
您还有一些错误,您使用了d两次。我知道您的问题所在。您尚未初始化char变量
char a = 'a';
char b = 'b';
char c = 'c';
我知道你有什么问题了。您尚未初始化char变量
char a = 'a';
char b = 'b';
char c = 'c';
你的
a
,b
,。。。这里有char
类型的变量,而不是实际的字符'a'
,'b'
,…此外,我认为您需要删除数值比较中的引号。您的a
,b
,。。。这里有char
类型的变量,而不是实际的字符'a'
,'b'
,…此外,我认为您需要删除数字比较中的引号。谢谢,我更改了字符a='a'。它似乎已经运行了,因为没有出现错误代码。我还注意到您使用了d
两次,并且从未使用f
。这就是你真正想要的吗?@史蒂文姆请考虑把这个答案标记出来,如果它回答了你的问题。否则,请澄清您的问题。我修复了“d”并标记了这些的已解决复选框,谢谢您的帮助。谢谢我更改了字符a='a'。它似乎已经运行了,因为没有出现错误代码。我还注意到您使用了d
两次,并且从未使用f
。这就是你真正想要的吗?@史蒂文姆请考虑把这个答案标记出来,如果它回答了你的问题。否则,请澄清您的问题。我修复了“d”并标记了这些问题的已解决复选框,感谢所有人的帮助