For loop help!!
Oct 15, 2016 at 3:16am UTC
I need help with my for loop. I want the user to enter a number between 1 and 5.
If they enter 2 then the for loop should print out
"What category for the 1st bunch: " and then take an input
"What category for the 2nd bunch: " and take one more input.
I Put my suffix code in a function at the bottom. I also added comments before the for loop to better explain what i am trying to do.
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
using namespace std;
int num_check(int num);
int num_check2(int num2);
void suff(int input, string x , string suff );
int main() {
int w = 0, x = 0, y = 0, z = 0;
int pretty_category, so_category, ugly_category;
int category;
string type;
cout << "How many flowers are in the Pretty Category?" << endl;
pretty_category = num_check(x);
cout << "How many flowers are in the So-So Category?" << endl;
so_category = num_check(y);
cout << "How many flowers are in the Ugly Category?" << endl;
ugly_category = num_check(z);
cout << "How many categories are in this basket" << endl;
category = num_check2(w);
//This is where the user will enter the numbers of categories they want
//if they enter five, then the for loop will print out "What category for the 1st bunch".
//this will repeat 5 times and each time it will have the correct suffix following the number.
for (int i = 1; i <= category; i++) {
}
return 0;
}
int num_check(int num) {
cin >> num;
while (num < 0) {
cout << "Error try again" << endl;
cin >> num;
}
return num;
}
int num_check2(int num2) {
cin >> num2;
while (num2 < 1 || num2>5) {
cout << "Error try again" << endl;
cin >> num2;
}
return num2;
}
void suff(int input, string x , string suff ) {
cout << "\nEnter a value (an input < 0 to exit): " << endl;
cin >> input;
if (input % 100 == 11 || input % 100 == 12 || input % 100 == 13) {
suff += "th" ;
cout << x << "" << input << suff;
}
else if (input % 10 == 1) {
suff += "st" ;
cout << x << "" << input << suff;
}
else if (input % 10 == 2) {
suff += "nd" ;
cout << x << "" << input << suff;
}
else if (input % 10 == 3) {
suff += "rd" ;
cout << x << "" << input << suff;
}
else if (input < 0) {
cout << "Have a nice day" << endl;
}
else {
suff += "th" ;
cout << x << "" << input << suff;
}
}
Oct 15, 2016 at 11:52am UTC
Use simple if-statements.
Oct 15, 2016 at 8:09pm UTC
Within the while loop or before
Oct 15, 2016 at 9:08pm UTC
Yea I got that part I just don't know how to use the suffix function within the for loop. I want it to print out "What category for the 1st part: ". It should have the correct suffix for each loop depending on the number they enter.
Oct 15, 2016 at 9:09pm UTC
Wait never mind, I think I know what you're saying.
Topic archived. No new replies allowed.