I am trying to write a small program that takes a string of positive integers and counts the to the end of the string using the most cost efficient route. It can only move 1 or 2 positions, so it can't jump from the first integer to the last. So 1 23 3 5 4. Anyhow, right now I am trying to figure out how to push the input string into a vector and I am running into trouble on lines 37-42 in my while loop.
#include <iostream>
#include <vector>
#include<string>
#include<sstream>
usingnamespace std;
/*void cost (int index)
{
if (index == gameBoard.back())
return gameBoard.back();
else
{
int costOne = cost (index + 1);
int costTwo = cost (index + 2);
return min(costOne,costTwo) + gameBoard[index];
}*/
int main()
{
char goAgain;
do{
cout << "Please enter the numbers of a game board on one line, separating numbers by spaces,then press the Enter key." << endl ;
string input;
getline (cin, input);
int i;
vector<string> gameBoard;
stringstream inputStringStream(input);
while (inputStringStream >> i)
{
gameBoard.push_back(inputStringStream);
for (int i = 0; i < gameBoard.size(); i++)
cout << gameBoard[i] << endl;
}
cout << "Would you like to enter a new Game Board (Y/N)?" << endl;
cin >> goAgain;
cin.ignore(9999, '\n');
}while(goAgain == 'Y'|| goAgain == 'y');
return 0;
}
Don't you think that you should be trying to push i? You know, the variable that you just stored the number you got from inputStringStream, instead of the stream itself?
EDIT: Actually, that doesn't make sense either, since gameBoard is a vector of strings, not int... Show an example of your input and what you want as output.
stringstream inputStringStream(input);
while (inputStringStream >> i)
{
gameBoard.push_back(input);
for (int i = 0; i < gameBoard.size(); i++)
cout << gameBoard[i] << endl;
I have this now and it works, but it prints way more copies than I anticipate I assume it's because I have i < gameBoard.size(), but I don't know a different way to put the statement, and I tried just printing the vector, but that crashes.
OK... I'm not sure why you want to use loops and vectors and stringstreams and stuff to do that.
1 2
getline(cin, input);
cout << input;
^Wouldn't that be enough? If you insist on using loops and vectors and stringstreams, this is how you do it:
1 2 3 4 5 6 7 8 9 10 11 12
getline (cin, input);
int i; //you really should rename this variable so you don't
//confuse it with the one that you use for the for loop
vector<int> gameBoard;
stringstream inputStringStream(input);
while (inputStringStream >> i)
gameBoard.push_back(i);
for (int i = 0; i < gameBoard.size(); i++)
cout << gameBoard[i] << ' ';
Well I have to put it into a vector, because it is a virtual "gameboard" and once I have the numbers stored in the vector I have to use my recursive function to count the most cost efficient route to the end of the gameboard. Right now I am just trying to have a program that runs properly and once I have that I will use the vector in my recursive function. Thanks for the help, do you know much about recursion?
I have most of it down now I am having trouble with lines 13-14, my recursion call. This is what I have and I am trying to make it so the function will return the lowest value each time it moves up the index, but it is limited to moving only 1 or 2 spaces forward at a time. Once it has figured this out it will return the lesser of the two values.
1 2 3 4 5 6 7 8 9 10 11 12 13
int cost (vector<int> gameBoard, int index)
{
if (index > 1)
{
int costOne = cost(index + 1);
int costTwo = cost(index + 2);
return min(costOne,costTwo) + gameBoard[index];
}
elseif (index == 0 || index == 1)
return index;
}
I don't understand how your function is supposed to do what you want it to do, even if it compiles. But as for why it's not compiling, lines 5 and 6 of the code you posted are wrong. Your function cost is supposed to take 2 parameters; you are only providing 1. Another thing is, how does your function know to stop when it hits the end of the vector?
int costOne = cost(gameBoard, (index-1));
int costTwo = cost(gameBoard, (index-2));
This compiles, but every time it's run it just prints 1, no matter the input.
I was assuming that once it was running that when it got to the index being equal to 1 or 0 it would end the recursion, but now that I type it, it sounds like it won't work.
So would I want it this way? Because when I try it this way it doesn't compile and when I have it as I do in my last post it compiles, but only ever returns 1.