2015-10-01 85 views
0

复杂的问题,可能很简单的答案。所以我需要制作的程序不能包含除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; 
} 
+2

你的问题具体是什么?这听起来像你基本上希望我们重写你的代码。 – CoryKramer

+3

而且?我在那里没有看到问题。 – NathanOliver

+1

当您使用调试器时,哪些语句导致问题?你确实运行了调试器并查看了变量,不是吗? –

回答

2
  1. 变化if (i = (amountofspots - 1))if (i == (amountofspots - 1))printVector()循环。

  2. tempvariable[i] = "-"更改为tempvariable[i] == '-'CreateVector()

=是赋值运算符,==是比较运算符。此外,单个字符被单引号包围,而不是双引号。

+0

我试过了,当我输入10,然后退出时,我仍然只接收1 –

+0

同时将'tempvariable [i] =“ - ”'改为'tempvariable [i] ==' - '' – drescherjm

+0

当我改变tempvariable [i ] ==“ - ”它输出一个错误。我认为它只需要= =?或者是1个字符串字符只是一个字符,我应该这样做' - ' –

0
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)); 

也许应该

void reverseVector(vector<int>& userinput) 
{ 
    int amountofspots = userinput.size(); 
    vector<int> tempvector; 
    for (int i = 1; i < amountofspots+1; i++) // Index error 
     tempvector.push_back(userinput.at(amountofspots - i)); 
+0

感谢您的帮助,但我非常确定矢量开始0和大小会返回元素的数量。所以在这种情况下,有四个元素将是0,1,2,3,但大小将是4.在这种情况下,循环应该正常工作不是吗? –

+0

@AaronJamesRasmussen:的确,元素索引是0,1,2,3,大小是4.但请注意,传递给at()的索引是'amountofspots-i'。因此循环中的索引将是4-1,4-2,4-3,4-4,即3,2,1,0。 –

+0

再一次你帮助heckl,再次感谢! –

0

以下:

 for(int i = 1; i <= tempvariable.size(); i++) 
     { 
      if (tempvariable[i] = "-" || isdigit(tempvariable[i])) 

必须成为:

 for(int i = 0; i < tempvariable.size(); i++) 
     { 
      if (tempvariable[i] == '-' || isdigit(tempvariable[i])) 

说明:

  • 字符串索引从0 开始和尺寸()结束 - 1
  • [i]返回单个char"-"不是一个单一的char,而是一个完整的字符串文字。您不能直接比较单个char与整个字符串。然而,-是一个单独的char,所以比较会起作用。
  • =不是比较而是分配。 ==用于比较。由于你的编译器应该警告你!

注意,有更多的问题与您的代码:

if (i = (amountofspots - 1)) 

你又混合赋值和比较。做那==。并注意编译警告!

最后,isdigit不是一个很好的功能。为了正确使用它,首先必须将操作数强制转换为int,并同时确保它不是无效值,如文档here所述。

如果指定的字符串不能被解析,为什么不抓住stoi抛出的异常呢?

+0

非常感谢我,尽管弦乐起源于1(它实际上是在我的教授的幻灯片中说的)<其他人帮助我,其余人像double equal和' - '这样的单引号。很多 –

+0

@AaronJamesRasmussen:真的吗?如果你的老师真的认为字符串索引从1开始,那么我会说他或她从来没有写过一个单一的C++(或C,或Java,或C#或PHP,或者无论)在他或她的整个生活中的节目...... :) –