I fixed the problems :
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
|
#include <iostream>
#include <string>
using namespace std;
string type; //declare string variable to hold string type
int num; //declare int variable to hold integer value
float flt; //declare float variable to hold float value
string str; //declare string varibale to hold string value
int sCount = 0; //declare and initialize int variable to hold number of string items
int iCount = 0; //declare and initialize int variable to hold number of integer items
int fCount = 0; //declare and initialize int variable to hold number of float items
//exception for missing value for int type
struct miss_val
{
int num;
miss_val(int mVal) { num = mVal; }
};
//exception for negative integer values
struct neg_int
{
int num;
neg_int(int negVal) { num = negVal; } //constructor fucntion
};
//exception for unrecognized type
struct wrong_type
{
string type;
wrong_type(string wType) { type = wType; } //constructor function
};
//function to read inputs summarize and output results
int readObjects(string type) {
if (type == "string") {
cin >> str; //get value of string
sCount++; //increment sCount for each string data in input
}
else if (type == "int") {
cin >> num;
if (num > 0) iCount++; //get value of int//increment iCount for each int data in input
if (num < 0) throw neg_int(num); //if negative value is entered throw an exception
if (!cin) throw miss_val(num); //if no integer is entered throw an exception
}
else if (type == "float") {
cin >> flt; //get value of float
fCount++; //increment fCount for each float data in input
}
else
throw wrong_type(type); //if uncrecognized type is entered throw exception
//output summary
cout << "int " << iCount << "; string " << sCount << "; float " << fCount << ";" << endl;
return 0;
}
//Function main reads data from the standard input
//then outputs a summary of how many data items of each type are in the input
int main() {
cin >> type;
while (cin){
try {
readObjects(type);
cin >> type;
}
catch (wrong_type) {
cerr << "Error: unrecognized type:" << type << "\n";
cin.clear();
cin >> type;
}
catch (neg_int) {
cerr << "Error: no integer should be negative:" << num << "\n";
cin.clear();
cin >> type;
}
catch (miss_val) {
cerr << "Error: missing value for int type:" << type << "\n";
cin.clear();
cin >> type;
}
}
return 0;
}
|
I still have some issues though; Here is my input:
int 3 int -2 string hello int float 3.2 adc float 2.2
Output:
int 1; string 0; float 0;
Error: no integer should be negative: -2
int 1; string 1; float 0;
Error: missing value for type int: int
int 1; string 1; float 1;
Error: unrecognized type: adc
int 1; string 1; float 2;
I would like my output to look like that :
Error: no integer should be negative: -2
Error: missing value for type int: int
Error: unrecognized type: adc
int 1; string 1; float 2;
I tried putting the cout in main fucntion outside while loop but the the output wouldnt display, it just output my errors and keeps reading