我正在做一个程序,计算来自不同类的两个测试的算术平均值。我向用户提供了命名他的班级的机会,名称可以是“ Math”或“ Computer Networks”。当我将其存储在数组位置时,第二个选项不起作用。换句话说:
我需要从键盘接收文本,这些文本有一些空格。然后,我必须将这些文本存储在数组的位置(由FOR(){}控制)中,这是一个字符串类型的数组。
如果我要问其他问题,我将不能在上面提到的字符串中插入带有重音符号的任何字符(例如“á”或“ç”)。即使我使用的是“ setlocale ...”,该程序也没有收到我希望字符串带有重音符号的名称(我的语言是葡萄牙语,并且具有这些类型的字符)。例如:当我在键盘上输入“Matemática”(英语为Math)时,下面的COUT不会显示“á”。
经过一些尝试,我正在使用Dev-C ++,但无法正常工作。我将展示卡住的那段代码。
#include <iostream>
#include <cstring>
#include <windows.h>
#include <clocale>
using namespace std;
int main() {
setlocale(LC_ALL, "");
int classesNumber = 5; // let's attribute this here only for this example
int classControl = 0;
float testNoteAV1[5]; // 5 just to this example
float testNoteAV2[5]; // 5 just to this example
string answer = "Y";
string className[5]; // 5 just to this example
/*
Below, in the "DO WHILE" structure, I'm trying to control the answer, defending the program from receive something different then Y or N
*/
for(int i = 0; i < classesNumber; i++) {
do{
if(classControl != 0) {
system("color F4");
cout << "\n\nINVALID ANSWER!\n\n"; Sleep(1000);
}
system("color 0A");
cout << "\nDo you want to name the " << i + 1 << "º class? [Y/N]: ";
cin >> answer;
answer[0] = toupper(answer[0]);
if(answer != "Y" && answer != "N")
classControl++;
}
while(answer != "Y" && answer != "N");
classControl = 0; // for the next 4 inputs
if(answer == "Y") {
cout << "Type the name of the " << i + 1 << "º class and then press ENTER (try not to accentuate it): ";
cin >> className[i]; /* ***Here is the problem I mentioned*** */
cout << "\n" << className[i] << ":\nAV 1: "; /* ***The output here isn't working when the class name has whitespaces and also the accents (á, ç, ü) is not appearing as them should*** */
cin >> testNoteAV1[i];
cout << "AV 2: ";
cin >> testNoteAV2[i];
}
else {
cout << "\n" << i + 1 << "º Class --> AV 1: ";
cin >> testNoteAV1[i];
cout << " AV 2: ";
cin >> testNoteAV2[i];
}
}
return 0;
}
我尝试了以下选项:
// 1º
getline(cin, className[i]);
// 2º
cin >> noskipws;
// 3º
cin.get(className[i]);
它们都不起作用,表现出不同的错误。