| | |
Help asap
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 61
Reputation:
Solved Threads: 0
anyone know why this isnt working?
the part where you enter
would you like to try again y/n)?
it works fine if i put y or Y but if i dont want to continue it wont terminate the program.
ex
For how many years have you collected rainfall data? -4
Invalid: number of years must be at least one.
Do you want to try again (y/n)? y
For how many years have you collected rainfall data? 0
Invalid: number of years must be at least one.
Do you want to try again (y/n)? y
For how many years have you collected rainfall data? 2
//Program continues as in Part 1
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { double totalRain = 0.0;// total rainfall inches double count = 0.0; int numData; char answer; cout <<"For how many years have you collected rainfall data? "; cin >> numData; if (numData < 1) { cout <<"Invalid: number of years must be at least one." << endl; cout <<"Do you want to try again (y/n)?"; cin >> answer; { if (answer == 'Y' || answer == 'y') cout <<"For how many years have you collected rainfall data? "; cin >> numData; if (answer != 'Y' || answer != 'y') cout <<"Goodbye"; } } for(int month = 0; month < numData*12; month++) { float inches; cout << "Enter rainfall (in inches) for month " << (month%12) + 1 << " of year " << (month/12) + 1 << ": "; cin >> inches; count++; totalRain += inches; } cout <<"The total number of months of rainfall data:" << numData * 12 << endl; cout <<"The total inches of rainfall for that period:" << totalRain << endl; cout <<"The average rainfall per month for that period:" << totalRain / (float)count << endl; system("pause"); return 0; }
the part where you enter
would you like to try again y/n)?
it works fine if i put y or Y but if i dont want to continue it wont terminate the program.
ex
For how many years have you collected rainfall data? -4
Invalid: number of years must be at least one.
Do you want to try again (y/n)? y
For how many years have you collected rainfall data? 0
Invalid: number of years must be at least one.
Do you want to try again (y/n)? y
For how many years have you collected rainfall data? 2
//Program continues as in Part 1
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
You didn't tell it to do so, that's why it would not terminate the program.
You should put your input validation in a loop though - what if the user enters two times consecutively a number of years <= 0 ?
Also you don't need count variable: you already have totalMonths (i.e. numData * 12).
c++ Syntax (Toggle Plain Text)
if(choice=='Y') { // do something } else { return EXIT_FAILURE; }
You should put your input validation in a loop though - what if the user enters two times consecutively a number of years <= 0 ?
Also you don't need count variable: you already have totalMonths (i.e. numData * 12).
Last edited by mrboolf; Dec 15th, 2008 at 8:09 pm.
•
•
Join Date: Jul 2008
Posts: 6
Reputation:
Solved Threads: 0
well hope this helps...i ddnt check the other guys...just wanted 2 do smting cuz i was bored...if u find something wrong tell me...thnx... xD
C++ Syntax (Toggle Plain Text)
#include <iostream.h> int nb_of_year; int i,j,n; double r_inches;//rainfall inches int totalm=0;//total months double total_i;//total inches double averagei; int main() { cout << "enter the number of years u wanna do the calculations for :\n"; cin >> n; for (i=1;i<=n;i++) { for (j=1;j<=12;j++) { cout << "Enter the rainfall (in inches) for month "<< j << " of year "; cout << i << ":\n"; cin >> r_inches; total_i+=r_inches; totalm+=1; } } cout << "The total number of months of rainfall data :" << totalm << endl; cout << "The total inches of rainfall for that period:" << total_i << endl; averagei=total_i/totalm; cout << "The average rainfall per month for that period is:" << averagei <<endl; return 0; }
![]() |
Similar Threads
- I feel sick... Hard drive not found... need help asap =( (Troubleshooting Dead Machines)
- Extracting folder information with c++ (C++)
- Need Help ASAP.. iMac(blue and white) wont eject the cd (Apple Hardware)
- Need Help ASAP.. iMac(blue and white) wont eject the cd (Troubleshooting Dead Machines)
- Need x86 assembly programmer ASAP ! (Assembly)
- Very New to Java and need help converting cubic inches to cm (Java)
Other Threads in the C++ Forum
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





