Recursion confusion

Mar 11, 2014 at 6:46pm
I'm really stuck on this one:

Write the definition of a function named copy that reads all the strings remaining to be read in standard input and displays them, one on a line with no other spacing, onto standard output .

I know I'm supposed to write a recursive function, but not sure how to do that with cin and cout. I got all the other ones right but this one is stumping me.
Mar 11, 2014 at 7:48pm
so far I got
1
2
3
4
5
6
7
8
9
void copy ()
{
	string x;
	cin >> x;
	
	if(cin.fail())
	cout << x <<endl;
	return(copy();
}
Mar 11, 2014 at 7:50pm
What do you mean by reading "all the string remaining"?
Mar 11, 2014 at 7:54pm
I guess it reads in a list of strings and then is supposed to output them all in one line with now spaces. That is just how it's stated, hence my confusion.
Mar 11, 2014 at 8:26pm
If that's the case then what you have is basically correct

You might want to just add the keyword "else" before return so that the recursion will break when someone enters a value that isn't a string.

Other than that, they will all display on one line continuously because there is no endl or \n, which is what you want.

Mar 11, 2014 at 8:36pm
This goes into a infinite loop when I submit it. It a stupid my programming lab assignment and it has to be done their way. I'm assuming I have a line that reads something like this "Here comes the sun". It wants me to read in each word to string x and then output the whole string.

Mar 11, 2014 at 9:04pm
Gotcha. Yeah I forgot with strings the only real way to make cin.fail return true is if you press Ctrl + z in the console and press enter. That will break the loop.

Is this what you're looking for:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<string>
using namespace std;

string copy (string x)
{
     string word;
	cin>>word;
	x=x+word+" ";

	if(cin.fail()){
	   return x;
	}

	else
	   return(copy(x));
}

int main(){
    string val="";
    cout<<copy(val)<<endl;
    system("pause");

}
Last edited on Mar 11, 2014 at 9:06pm
Mar 11, 2014 at 9:09pm
Mostly.. apparently it is supposed to be void and can't accept any parameter. This is the dumbest assignment ever. How would you just recursively call the cin function?
Mar 11, 2014 at 9:34pm
Well you basically are already recursively calling the cin function because every time the function is called, you have to cin a value. Are you allowed to use global variables?
Mar 11, 2014 at 9:49pm
1
2
3
#include <iostream>
using namespace std;
void copy();


That code is what is within My Programming Lab. Other than the brief sentence in the first post the only thing is says I cannot do is use a loop (obviously... it has to be recursive).
Mar 11, 2014 at 9:59pm
I'll be honest with you, I don't know how to do it without a parameter or a global variable. Here's how to go about it using globals:

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
#include<iostream>
#include<string>
using namespace std;

string x;
string word;
void copy ()
{
	cin>>x;
     word+=x+" ";
	
	if(cin.fail()){
	   cout<<word<<endl;
	}

	else
	   return(copy());
}

int main(){
  
    copy();
    cout<<endl;
    system("pause");

}


Technically, the program is still void and accepts no parameters so..
Last edited on Mar 11, 2014 at 9:59pm
Mar 11, 2014 at 10:08pm
Still not it... Thank you for your help.
Mar 11, 2014 at 10:34pm
You could try something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

void copy()
{
    std::string str;
    if (!(std::cin >> str))
        return;
    std::cout << str << '\n';
    copy();
}

int main()
{
    copy();
}

Keep in mind that if the input is coming from the user (the keyboard), then the condition on line 7 will never be satisfied, and it'll just keep on asking for more input.
So I would either get the input from a file instead, or just enter a EOF character (Ctrl+Z on a blank line in Windows; not sure about other OSs) when you're done entering your input.

This shows the former method:
http://coliru.stacked-crooked.com/a/03a0cb2782ef938b
Topic archived. No new replies allowed.