Recursive Function help/ Vector

Pages: 12
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <vector>
#include<string>
#include<sstream>

using namespace 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;
}
Your problem is on line 39. What do you think you're trying to push?
The input string into the vector is what I am trying to have it push.
Ah I didn't have input. I had inputStringStream.
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.
Last edited on
I tried, but it wouldn't work either.
It gives more errors

1
2
3
4
5
6
7
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.
fg109 wrote:
Show an example of your input and what you want as output.
Please enter the number of a Gameboard on one line, separate the numbers by spaces, when finished press the Enter key.

1 2 3 4 5

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
...it prints 15 lines of the same thing.
I'm asking what you want it to print out, not what it is printing out.
Oh sorry.

Input: 1 2 3 4 5

Output: A single line 1 2 3 4 5.

I am just trying to get everything correct before I start on the recursion function.
Last edited on
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] << ' ';


^Changed the relevant parts of your code.
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];
            }

            else if (index == 0 || index == 1)
                return index;
        }
Last edited on
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?
So would I want something more like this?

1
2
 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.
Last edited on
No, it'll work. It didn't make sense before because you had index + 1 instead of index - 1 which meant it would have kept increasing without limits.
int costOne = cost(gameBoard.at(index-1));

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.
To print what the function is returning I would just have

cout<<cost;

right?
If your function has parameters, then you need that too.

cout << cost(something);
Pages: 12