FUnction Help
I want to create a function that makes sure the input is positive. Im not sure if i am on the right track.
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
|
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
using namespace std;
int main() {
double pretty_category,so_category,ugly_category;
cout << "How many flowers are in the Pretty Category?" << endl;
cin >> pretty_category;
cout << "How many flowers are in the So-So Category?" << endl;
cin >> so_category;
cout << "How many flowers are in the Ugly Category?" << endl;
cin >> ugly_category;
system("pause");
return 0;
}
void number_checker() {
double num;
if (num < 0) {
cin.ignore(INT_MAX, '/n');
}
else {
cin >> num;
}
}
|
1 2 3 4 5 6 7
|
bool number_positive(int number) {
if (number < 0) {
return false;
}
return true;
}
|
Try that.
I tried this but im not sure why its not working.
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
|
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
using namespace std;
void num_check(double num);
int main() {
double pretty_category,so_category,ugly_category;
cout << "How many flowers are in the Pretty Category?" << endl;
num_check(pretty_category);
cout << "How many flowers are in the So-So Category?" << endl;
cin >> so_category;
cout << "How many flowers are in the Ugly Category?" << endl;
cin >> ugly_category;
system("pause");
return 0;
}
void num_check(double num) {
cin >> num;
while (num < 0) {
cout << "Error try again" << endl;
cin >> num;
}
return ;
}
|
Topic archived. No new replies allowed.