Wanting to print all inputs
Oct 13, 2020 at 7:57pm UTC
Howdy, I am wanting to print all these choices. I set them to 4, and at the end of the 4 choices, I would like to print all the choices made. How can I go about that? Thanks
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 59 60 61 62 63 64 65 66 67 68
#include <cctype>
#include <iostream>
#include <limits>
#include <string>
#include <vector>
#include <ctime>
int main()
{
char choice {};
int n;
for (n=0; n<4; n++)
{
std::cout<<"\n1 - Man" ;
std::cout<<"" <<std::endl;
std::cout<<"\n2 - Woman" ;
std::cout<<"" <<std::endl;
std::cout<<"\n3 - Aunt" ;
std::cout<<"" <<std::endl;
std::cout<<"\n4 - Uncle" ;
std::cout<<"" <<std::endl;
std::cout<<"\nEnter Selection: " ;
std::cin >> choice;
std::cout<<"" <<std::endl;
choice = std::toupper(choice);
switch (choice)
{
case '1' :
std::cout<<"You picked me!" ;
std::cout<<"" <<std::endl;
break ;
case '2' :
std::cout<<"You picked me!" ;
std::cout<<"" <<std::endl;
break ;
case '3' :
std::cout<<"Hello, thanks" ;
std::cout<<"" <<std::endl;
break ;
case '4' :
std::cout<<"Hello, I am voting GOP" ;
std::cout<<"" <<std::endl;
break ;
default :
std::cout << "Invalid choice\n" ;
break ;
}
}
}
Oct 14, 2020 at 6:16am UTC
Turn
choice
into an array and introduce a second loop. Like so:
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 59 60 61 62 63 64 65 66 67 68
#include <cctype>
#include <iostream>
#include <limits>
#include <string>
#include <vector>
#include <ctime>
int main()
{
char choice[4] {}; // You may use a vector
int n;
for (n=0; n<4; n++)
{
std::cout<<"\n1 - Man" ;
std::cout<<"" <<std::endl;
std::cout<<"\n2 - Woman" ;
std::cout<<"" <<std::endl;
std::cout<<"\n3 - Aunt" ;
std::cout<<"" <<std::endl;
std::cout<<"\n4 - Uncle" ;
std::cout<<"" <<std::endl;
std::cout<<"\nEnter Selection: " ;
std::cin >> choice[i]; // Note:[i]
std::cout<<"" <<std::endl;
choice[i] = std::toupper(choice[i]); // Note:[i]
switch (choice[i]) // Note:[i]
{
case '1' :
std::cout<<"You picked me!" ;
std::cout<<"" <<std::endl;
break ;
case '2' :
std::cout<<"You picked me!" ;
std::cout<<"" <<std::endl;
break ;
case '3' :
std::cout<<"Hello, thanks" ;
std::cout<<"" <<std::endl;
break ;
case '4' :
std::cout<<"Hello, I am voting GOP" ;
std::cout<<"" <<std::endl;
break ;
default :
std::cout << "Invalid choice\n" ;
break ;
}
}
for (n=0; n<4; n++)
{
// print choice[i]
}
}
Oct 14, 2020 at 1:17pm UTC
Ok, I got that to work, but it only prints the number I inputed. How can I have it print all of it?
So it prints...
Man
Aunt
Uncle
Woman
or whatever order they entered it.
Oct 14, 2020 at 2:20pm UTC
In the second loop you can basically use the same kind of switch:
1 2 3 4 5 6 7 8 9 10 11 12 13
switch (choice[i]) // Note:[i]
{
case '1' :
std::cout<<"Man" ; // You picked me!
std::cout<<"" <<std::endl;
break ;
case '2' :
std::cout<<"Woman" ; // You picked me!
std::cout<<"" <<std::endl;
break ;
...
}
Oct 14, 2020 at 2:20pm UTC
Hello jax16,
Is this what you are looking for?
1 - Man
2 - Woman
3 - Aunt
4 - Uncle
Enter Selection: 1
You picked me!
1 - Man
2 - Woman
3 - Aunt
4 - Uncle
Enter Selection: 2
You picked me!
1 - Man
2 - Woman
3 - Aunt
4 - Uncle
Enter Selection: 1
You picked me!
1 - Man
2 - Woman
3 - Aunt
4 - Uncle
Enter Selection: 3
Hello, thanks
Choice #1 picked 2 times.
Choice #2 picked 1 times.
Choice #3 picked 1 times.
Choice #4 picked 0 times.
Andy
Edit:
Last edited on Oct 14, 2020 at 2:21pm UTC
Oct 14, 2020 at 4:26pm UTC
From jax16 here
http://www.cplusplus.com/forum/general/273409/
Ok, I got that to work, but it only prints the number I inputed. How can I have it print all of it?
So it prints...
Man
Aunt
Uncle
Woman
or whatever order they entered it.
Oct 14, 2020 at 5:08pm UTC
Is this what you want??
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
#include <iostream>
#include <limits>
int main()
{
constexpr int numask = 4;
constexpr const char * relat[] = {"Man" , "Woman" , "Aunt" , "Uncle" };
constexpr const char * desc[] = {"You picked me!" , "You picked her" , "Hello, thanks" , "Hello, I am voting GOP" };
static_assert (std::size(relat) == std::size(desc));
int totals[std::size(relat)] {};
for (int n = 0; n < numask; ++n) {
char choice {};
for (int r = 0; r < std::size(relat); ++r)
std::cout << r + 1 << " - " << relat[r] << std::endl;
while (((std::cout << "\nEnter Selection: " ) && !(std::cin >> choice)) || choice < '1' || choice > '1' + std::size(relat) - 1) {
std::cout << "Invalid selection\n" ;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );
std::cout << std::endl;
std::cout << desc[choice - '1' ] << '\n' ;
++totals[choice - '1' ];
}
std::cout << '\n' ;
for (int t = 0; t < std::size(totals); ++t)
std::cout << relat[t] << " picked " << totals[t] << '\n' ;
}
1 - Man
2 - Woman
3 - Aunt
4 - Uncle
Enter Selection: 1
You picked me!
1 - Man
2 - Woman
3 - Aunt
4 - Uncle
Enter Selection: 2
You picked her
1 - Man
2 - Woman
3 - Aunt
4 - Uncle
Enter Selection: 2
You picked her
1 - Man
2 - Woman
3 - Aunt
4 - Uncle
Enter Selection: 4
Hello, I am voting GOP
Man picked 1
Woman picked 2
Aunt picked 0
Uncle picked 1
Topic archived. No new replies allowed.