Home › Forums › C Programming › Question about my program code
- This topic has 0 replies, 1 voice, and was last updated 17 years, 1 month ago by happyherlean.
- AuthorPosts
- November 1, 2007 at 8:20 pm #2023happyherleanParticipant
Hi!
I’m a beginner in C++ The program below inputs sevaral positive numbers with decimal points, analyzes these numbers, and outputs the result. The program works fine. But I wanted to make sure that a user puts only positive numbers or -1 to end repetition loop. So, I created couple of functions that would check the numbers, return them in case if they are positive or equal -1. In case they are negative, the program would output warning message and ask to enter the number again. It also work fine, but if I enter negative number and than enter positive number, the program saves negatives number, although I return the value from the function. Can anyone please explain me where my error is. Thanks!P.S. I know that it’s possible to use template functions when you have a function that does the same operations, but with different types of variables. Can I use this concept with my problem when I have similar functions, but different names of variables? If yes, can anyone please show me how? Thanks!
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130#include <cstdlib><br />#include <iostream><br />#include <iomanip><br />using namespace std;<br /><br /><br />int get_acc_number(int);<br />float get_b_balance(float);<br />float get_charges(float);<br />float get_credits(float);<br />float get_cr_limit(float);<br />void text(void);<br /><br /><br /><br />int main(int argc, char *argv[])<br />{<br />int acc_number = 0;<br />float b_balance, charges, credits, cr_limit, balance;<br /><br />while (acc_number != -1)<br />{<br />cout << "Enter account number (-1 to end): ";<br />cin >> acc_number;<br />get_acc_number(acc_number);<br />if (acc_number != -1)<br />{<br />cout << setiosflags (ios::fixed | ios::showpoint | ios::left);<br />cout << setprecision (2) << "Enter beginning balance: ";<br />cin >> b_balance;<br />get_b_balance(b_balance);<br />cout << "Enter total charges: ";<br />cin >> charges;<br />get_charges(charges);<br />cout << "Enter total credits: ";<br />cin >> credits;<br />get_credits(credits);<br />cout << "Enter credit limit: ";<br />cin >> cr_limit;<br />get_cr_limit(cr_limit);<br />balance = b_balance + charges - credits;<br />cout << "n";<br />if (balance > cr_limit)<br />{<br />cout << setiosflags (ios::fixed | ios::showpoint | ios::left);<br />cout << "Account: " << setw (15) << acc_number << endl;<br />cout << "Credit limit: " << setw (15) << cr_limit << endl;<br />cout << "Balace: " << setw (15) << balance << endl;<br />cout << "Credit Limit exeededn" << endl;<br />}<br /><br />}<br />}<br /><br />system("PAUSE");<br />return EXIT_SUCCESS;<br />}<br /><br />int get_acc_number(int acc_number)<br />{<br />if (acc_number == -1 || acc_number >= 0)<br />return acc_number;<br />else<br />{<br />text();<br />cout << "Enter account number (-1 to end): ";<br />cin >> acc_number;<br />return get_acc_number(acc_number); <br />} <br />}<br />float get_b_balance(float b_balance)<br />{<br />if (b_balance >= 0)<br />return b_balance;<br />else<br />{<br />text();<br />cout << "Enter beginning balance: ";<br />cin >> b_balance;<br />return get_b_balance(b_balance); <br />} <br /><br />}<br />float get_charges(float charges)<br />{<br />if (charges >= 0)<br />return charges;<br />else<br />{<br />text();<br />cout << "Enter total charges: ";<br />cin >> charges;<br />return get_charges(charges); <br />} <br /><br />}<br />float get_credits(float credits)<br />{<br />if (credits >= 0)<br />return credits;<br />else<br />{<br />text();<br />cout << "Enter total credits: ";<br />cin >> credits;<br />return get_credits(credits); <br />} <br /><br />}<br />float get_cr_limit(float cr_limit)<br />{<br />if (cr_limit >= 0)<br />return cr_limit;<br />else<br />{<br />text();<br />cout << "Enter credit limit: ";<br />cin >> cr_limit;<br />return get_cr_limit(cr_limit); <br />} <br /><br />}<br />void text(void)<br />{<br />cout << "The number you entered is less than 0, "<br /><< "and, therefore invalid. "<br /><< "nPlease, try againn"<br /><< "n";<br />}<br /></iomanip></iostream></cstdlib>
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.