0

output order

i want to how to make the output dependent on its each individual inpu #include <iostream> #include <cstdlib> #include <cmath> #include <ctime> using namespace std; int main() { srand(time(0)); int r = rand(); char a,e,i,o,u; int c = r % 40 + 1; int v = r % 40 + 41; int b = r % 40 + 81; int n = r % 40 + 121; int m = r % 40 + 161; cout <<"This program plays a simple random number game.\n"; cout <<"Enter 5 vowel characters(a,e,i,o,u) separated by space: "; if (cin>>a) a = c ; if (cin>>e) e = v ; if (cin>>i) i = b; if (cin>>o) o = n; if (cin>>u) u = m; cout <<"The random numbers are "<< c <<" "<<v <<" "<< b<<" " << n<<" " << m; return 0; } Edit & Run if you run this those codes , you can see that when enter 5 different vowels a, e ,i , o ,u in different position like e ,i ,a u, o the output of the program still stick to the format "a, e,i o ,u" not "e ,i , a ,u ,o" so is there anyway so that outputs of this are in same position as e,i,a,u,o not a,e,i,o,u.

24th Oct 2017, 6:41 PM
Anh Lam
Anh Lam - avatar
2 Respostas
+ 5
Of course ! Try something like that : char c; int vals[5]; try{ for(unsigned i = 0; i < 5; ++i){ cin >> c; switch(c){ case 'a': vals[i] = r%40 + 1; break; case 'e': vals[i] = r%40 + 41; break; case 'i': vals[i] = r%40 + 81; break; case 'o': vals[i] = r%40 + 121; break; case 'u': vals[i] = r%40 + 161; break; } } }catch(...){ cout << "failed to properly read your input" << endl; return -1; } for(unsigned i = 0; i < 5; ++i) cout << vals[i] << ' '; cout << '\n';
24th Oct 2017, 7:38 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 4
You are changing a, e, i, o, and u to be the values c, v, b, n, and m respectively without looking at their value. Then, you ignore them completely to print out the values c, v, b, n, and m so your output will always be in order. @Baptiste code makes more sense and is likely what you wanted.
24th Oct 2017, 10:40 PM
John Wells
John Wells - avatar