gpt4 book ai didi

c++ - 如何使用 CIN 在一行中捕获两种不同的数据类型?

转载 作者:行者123 更新时间:2023-11-28 00:29:53 25 4
gpt4 key购买 nike

这是一项家庭作业:基本上,我需要使用 cin 行来捕获一行,例如:
玛丽史密斯 10 9 10 3 100 8 7.5 10 73 9 10 5 9 87 -1

...然后将名称放入字符串 vector 中,将数字(成绩)放入具有相同索引的多维 vector 中。

到目前为止我有这个:

#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <vector>
#include <string>
using namespace std;

vector<string> names;
vector< vector<float> > grades; //Multidimensional vector for grades
string tempName;
float tempGrade;
int student = 0;

int main(){
do {
cin >> tempName; //Get the first entry before whitespace
names.push_back(tempName); //Push the name into the vector

//For all the other inputs, put the respective grades at the same base index?
for (int i = 0; tempGrade > 0; ++i) {
cin >> tempGrade; //Get all grades until -1
grades[student][i].push_back(tempGrade);//Add grade to the index.
}
++student;
} while (tempName != "KEY"); //If you see KEY, kill the program.
return 0;
}

不幸的是,问题在于机器将所有条目视为字符串。为什么它不开始迭代我的 for 循环?

更新
之前我说过它需要在多维数组中,但我的意思是 vector 。我在上面更新了这个。

最佳答案

您的 tempGrade 变量开始时为 0

关于c++ - 如何使用 CIN 在一行中捕获两种不同的数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23357885/

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