2015-10-01 84 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,或者無論)在他或她的整個生活中的節目...... :) –