2017-05-21 46 views
0

FizzBu​​zz程序。用戶輸入用逗號分隔的數字。該程序讀取輸入並讓計算機知道是否可以被3,5或兩者整除。當用戶輸入15,5,30時,程序將只輸出第一個數字15,並在那裏停止。我究竟做錯了什麼?將字符串轉換爲整數向量

void processVector(vector<int> intVector) 
{ 
    bool loop; 
    for (int i = 0; i < intVector.size(); i++) 
    { 
     loop = true; 
    } 
} 

int main() 
{ 

    cout << "Welcome to the FizzBuzz program!" << endl; 

    cout << "This program will check if the number you enter is divisable by 
      3, 5, or both." << endl; 


    cout << "Please enter an array of numbers separated by a comma like so, 
      5,10,15" << endl; 
    cin >> userArray; 

    vector<int> loadVector(string inputString); 
    istringstream iss(userArray); 
    vector <int> v; 

    int i; 

    while (iss >> i); 
    { 
     v.push_back(i); 
     if (iss.peek() == ',') 
      iss.ignore(); 


     if (i % 15 == 0) 
     { 
      cout << "Number " << i << " - FizzBuzz!" << endl; 

     } 
     else if (i % 3 == 0) 
     { 
      cout << "Number " << i << " Fizz!" << endl; 

     } 

     else if (i % 5 == 0) 
     { 
      cout << "Number " << i << " Buzz!" << endl; 

     } 

     else 
     { 
      cout << "Number entered is not divisable by 3 or 5." << endl;  
     } 
    } 

    system("pause"); 

} 
+0

請編輯您的問題包含[MCVE] – Slava

+0

中你做錯了的事情很肯定的:不包含頭文件,未申報'userArray'。當與一個空的身體循環。當你不需要它時調用'system()'。聲明'loadVector()',但從不調用它。定義'processVector()',但從不以任何方式使用它。 –

+0

@ Ben Voigt,謝謝,本,我確實有頭文件,我沒有在文章中完整複製代碼。頭文件我已經是.... 的#include 「stdafx.h中」 的#include 的#include 的#include 的#include 的#include ...... 我會在休息。再次感謝您的幫助 – Ang316

回答

0

這裏是我會怎麼處理這個問題:

#include <iostream> 
#include <string> 
#include <vector> 

int main() { 
    std::cout << "!!!Hello World!!!" << std::endl; // prints !!!Hello World!!! 
    std::cout << "Please enter your numbers seperated by a comma (5, 3, 5, 98, 278, 42): "; 

    std::string userString; 
    std::getline(std::cin, userString); 
    std::vector<int> numberV; 

    size_t j = 0; // beginning of number 
    for(size_t i = 0; i < userString.size(); i++){ 
     if((userString[i] == ',') || (i == userString.size() -1)){ // could also use strncmp 
      numberV.push_back(std::stoi(userString.substr(j, i))); // stoi stands for string to int, and .substr(start, end) creates a new string at the start location and ending at the end location 
      j = i + 1; 

     } 
    } 

    for(size_t n = 0; n < numberV.size(); n++){ 
     std::cout << numberV[n] << std::endl; 
    } 

    return(0); 

}

這應該給你解決問題(無處理程序的fizzbuzz一部分)的方法,我個人覺得更簡單。

使用功能的基本形式是:

<return type> <function_name(<inputs)>{ 
    stuff 
}; 

因此,一個基本功能是將一個字符串返回一個向量(你想要什麼)將是:

std::vector myStringToVector(std::string inputString){ 
    std::vector V; 
    // your code (see the prior example for one method of doing this) 
    return(V); 
}; 

它還看起來像他們想要一個單獨的功能輸出你的矢量值,這可能看起來像這樣:

void myVectorPrint(std::vector inputVector){ 
    // your code (see prior example for a method of printing out a vector) 
}; 
+0

@Aarin Covrig謝謝,看起來更簡單,但是,根據說明,我必須包含兩個函數,如 創建一個將輸入字符串轉換爲整數向量的函數。該功能必須如下所示: vector loadVector(string inputString) 編寫另一個函數,該函數會根據第2周定義的FizzBu​​zz規則輸出處理整數向量的結果。根據第2周的作業。函數應該看起來像這樣: void processVector(vector intVector) – Ang316

+0

您可以輕鬆修改代碼以在函數中工作,您可以將代碼從'j'的定義開始並結束於for循環的結尾並將其放入一個接受一個字符串並返回一個向量的函數。我相信你的流程向量函數也需要工作,看起來你應該在你的打印語句裏面打個招呼。 –

+0

我已經添加了更多的信息和一些僞代碼,你可以使用它提供的代碼的其餘部分來解決你的問題,因爲這是作業,我不會完全爲你解決它(但是,所有你真正需要做的就是將其分開,並將其納入這些情侶功能以滿足您的教練要求)。 –

0

謝謝@Aaron的幫助。這是完成的代碼,它非常棒! 我不得不多花一點時間研究幾件事情,並試圖瞭解哪些順序以及如何在函數中以及如何調用它們。我欣賞所有的幫助,因爲我說我是一個noob。

#include "stdafx.h" 
#include <iostream> 
#include<sstream> 
#include<string> 
#include<vector> 

using namespace std; 


vector<int> loadVector(string inputString) 
{ 
stringstream ss(inputString); 
vector <int> numberV; 

int n; 

size_t j = 0; // beginning of number 
for (size_t n = 0; n < inputString.size(); n++) 
{ 
    if ((inputString[n] == ',') || (n == inputString.size() - 1)) 
    { 
     numberV.push_back(std::stoi(inputString.substr(j, n))); 
     j = n + 1; 

    } 

} 


return numberV; 
} 
void processVector(vector<int> intVector) 
{ 
for (int i = 0; i < intVector.size(); i++) 
{ 
    int n = intVector.at(i); 
    if (n % 15 == 0) 
    { 
     cout << "Number " << n << " - FizzBuzz!" << endl; 
    } 
    else if (n % 3 == 0) 
    { 
     cout << "Number " << n << " Fizz!" << endl; 
    } 

    else if (n % 5 == 0) 
    { 
     cout << "Number " << n << " Buzz!" << endl; 
    } 

    else 
    { 
     cout << "Number entered is not divisable by 3 or 5." << endl; 
    } 
} 
} 

int main() 
{ 
cout << "Welcome to the FizzBuzz program." << endl 
    << "Please enter an array of numbers separated by comma's (5, 10, 15)" 
    << endl; 

string inputString; 
getline(cin, inputString); 

try 
{ 
    vector<int> intVector = loadVector(inputString); 
    processVector(intVector); 
} 
catch (const exception& e) 
{ 
    cout << "Exception caught: '" << e.what() << "'!;" << endl; 
} 
system("pause"); 
return 0; 
}