簡體   English   中英

將字符串轉換為整數向量

[英]Converting String to vector of Integers

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

}

這是解決問題的方法:

#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)
};

謝謝@Aaron的幫助。 這是完成的代碼,效果很好! 我不得不花更多的時間研究一些東西,並試圖了解在函數以及如何調用它們方面哪個順序和放置什么位置。 我感謝所有幫助,因為我說我是菜鳥。

#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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM