getline Problem

I had to change from using cin >> to using getline(cin,s) because it would only take the first word as input if there were spaces. Now the application is just infinitely outputting whatever the user inputs when there are spaces. Do I need to make it ignore spaces? How do I fix it so that it takes all of the input when there are spaces? My full code is below. Thanks in advance for help!

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
#include <stdafx.h>
#include <cctype>    
#include <iostream>
#include <string>
using namespace std;
void permutation(string s, int i, int n)

{

	int j;

	if (i == n)

		cout << s << "\t";

	else

	{

		for (j = i; j < s.length(); j++)

		{

			swap(s[i], s[j]);

			permutation(s, i + 1, n);

			swap(s[i], s[j]);

		}
	}
}
int main()
{
	string s;
	cout << "Paste the ciphertext and press enter to view all permutations : ";

	getline(cin, s);

	cout << endl << "Possible Outcomes : " << endl;

	permutation(s, 0, s.length() - 1);
	

	cout << endl;
	cin.ignore(10000);
	cout << "Press Enter to Exit";

}

Seems to me the problem may be line 12.
Cannot reproduce the problem. Show example of your input. Note that your program will take several seconds to process even 7 characters lines so if you have larger input, it just wasn't processesed fully yet.
Last edited on
How do I fix it so that it takes all of the input when there are spaces?

Out of curiousity nosiness, how long a string did you enter?

Andy

RAN

input = "to be"
chars = 5
count = 5! = 120
secs = 0.016

input = "to be "
chars = 6
count = 6! = 720
secs = 0.062

input = "to be o"
chars = 7
count = 7! = 5040
secs = 0.375

input = "to be or"
chars = 8
count = 8! = 40320
secs = 3.546

input = "to be or "
chars = 9
count = 9! = 362880
secs = 28.796

EST (assuming same rate, approx 7.93541e-005 secs a term)

input = "to be or not"
chars = 12
count = 12! = 479001600
secs = 38010.7 = approx 38011

i.e. 10 hours 33 minutes 31 seconds

input = "to be or not to be that is the question"
chars = 39
count = 39! = 20397882081197443358640281739902897356800000000
http://www.tsm-resources.com/alists/fact.html
= approx 2.0398e+46
secs = 1.61866e+042 secs

CF

Age of Universe = approx 13.82 billion years = 13.82e+9 years
1 year = 3.15569e7 secs
Age of Universe = 436116358000000000 = 4.36116358e+17

So time to output all perms of "to be or not that is the question" (on my PC) is approx 3.71152e+024 times the age of the Universe.
Last edited on
The columns in the table are:
- N (length of sentence)
- N!
- time to print all perms
- time / age of universe

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

double factorial(int n) {
    double ret = 1.0;
    for(int i = 1; i <= n; ++i)
        ret *= static_cast<double>(i);
    return ret;
}

int main()
{
    const double year_in_secs = 3.15569e7;
    const double age_universe_years = 13.82e+9;
    const double age_universe_sec = age_universe_years * year_in_secs;

    const double output_rate = 7.93541e-005;

    cout << "output_rate  = " << output_rate << "\n";
    cout << "age_universe = " << age_universe_sec << " secs\n";
    cout << "\n";

    for(int n = 1; n <= 39; ++n) {
        double f = factorial(n);
        double t = f * output_rate;
        cout << setw( 2) << n << "  "
             << setw(12) << f << "  "
             << setw(12) << t << "   "
             << setw(12) << t / age_universe_sec << "\n";
    }

    return 0;
}


output_rate  = 7.93541e-005
age_universe = 4.36116e+017 secs

 1             1  7.93541e-005   1.81956e-022
 2             2   0.000158708   3.63913e-022
 3             6   0.000476125   1.09174e-021
 4            24     0.0019045   4.36695e-021
 5           120    0.00952249   2.18348e-020
 6           720      0.057135   1.31009e-019
 7          5040      0.399945    9.1706e-019
 8         40320       3.19956   7.33648e-018
 9        362880        28.796   6.60283e-017
10   3.6288e+006        287.96   6.60283e-016
11  3.99168e+007       3167.56   7.26311e-015
12  4.79002e+008       38010.7   8.71573e-014
13  6.22702e+009        494140   1.13305e-012
14  8.71783e+010  6.91795e+006   1.58626e-011
15  1.30767e+012  1.03769e+008    2.3794e-010
16  2.09228e+013  1.66031e+009   3.80703e-009
17  3.55687e+014  2.82253e+010   6.47196e-008
18  6.40237e+015  5.08055e+011   1.16495e-006
19  1.21645e+017  9.65304e+012   2.21341e-005
20   2.4329e+018  1.93061e+014    0.000442682
21  5.10909e+019  4.05428e+015     0.00929632
22    1.124e+021  8.91941e+016       0.204519
23   2.5852e+022  2.05146e+018        4.70394
24  6.20448e+023  4.92351e+019        112.894
25  1.55112e+025  1.23088e+021        2822.36
26  4.03291e+026  3.20028e+022        73381.4
27  1.08889e+028  8.64076e+023    1.9813e+006
28  3.04888e+029  2.41941e+025   5.54763e+007
29  8.84176e+030   7.0163e+026   1.60881e+009
30  2.65253e+032  2.10489e+028   4.82644e+010
31  8.22284e+033  6.52516e+029    1.4962e+012
32  2.63131e+035  2.08805e+031   4.78783e+013
33  8.68332e+036  6.89057e+032   1.57998e+015
34  2.95233e+038  2.34279e+034   5.37195e+016
35  1.03331e+040  8.19978e+035   1.88018e+018
36  3.71993e+041  2.95192e+037   6.76865e+019
37  1.37638e+043  1.09221e+039    2.5044e+021
38  5.23023e+044   4.1504e+040   9.51672e+022
39  2.03979e+046  1.61866e+042   3.71152e+024

Last edited on
Topic archived. No new replies allowed.