复杂的问题,可能很简单的答案。所以我需要制作的程序不能包含除String,iostream和vector之外的任何库。我需要创建一个具有3个功能的程序。一个创建一个整数矢量,一个反转矢量,一个打印矢量。为了获取值,我需要使用getline来获取字符串,如果字符串状态退出,我们会停止向其中添加新值。其他方面,我们需要测试一个整数(正数或负数)并将其添加到矢量中。我的代码开始变得复杂,所以我真的需要一些帮助。以下是我到目前为止。如果这很重要,我也使用Visual Studio。感谢您提前提供任何帮助!我的问题是当我运行该程序时,它只会输出第一个数字。我不知道为什么,并想知道我做错了什么。使用矢量C++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<int> CreateVector()
{
string tempvariable;
bool quit = false;
vector<int> userinput;
cout << "Please enter in an integer, type 'quit' to exit " << endl;
while (!quit)
{
getline(cin, tempvariable);
if (tempvariable == "quit")
quit = true;
else
{
bool isaninteger = true;
for(int i = 1; i <= tempvariable.size(); i++)
{
if (tempvariable[i] = "-" || isdigit(tempvariable[i]))
continue;
else
{
cout << "You entered in an incorrect option, please enter in a correct option" << endl;
cin.clear();
cin.ignore();
isaninteger = false;
break;
}
}
if (isaninteger)
userinput.push_back(stoi(tempvariable));
cout << "Please enter in an integer, type 'quit' to exit " << endl;
}
}
return userinput;
}
void printVector(vector<int> userinput)
{
int amountofspots = userinput.size();
cout << "Your Vector is ";
for (int i = 0; i < amountofspots; i++)
{
if (i = (amountofspots - 1))
cout << userinput.at(i) << endl;
else
cout << userinput.at(i) << " , ";
}
}
void reverseVector(vector<int>& userinput)
{
int amountofspots = userinput.size();
vector<int> tempvector;
for (int i = 0; i < amountofspots; i++)
tempvector.push_back(userinput.at(amountofspots - i));
for (int i = 0; i < amountofspots; i++)
userinput.pop_back();
for (int i = 0; i < amountofspots; i++)
userinput.push_back(tempvector.at(i));
}
int main()
{
vector<int> CreatedVector = CreateVector();
printVector(CreatedVector);
reverseVector(CreatedVector);
printVector(CreatedVector);
system("pause");
return 0;
}
你的问题具体是什么?这听起来像你基本上希望我们重写你的代码。 – CoryKramer
而且?我在那里没有看到问题。 – NathanOliver
当您使用调试器时,哪些语句导致问题?你确实运行了调试器并查看了变量,不是吗? –