The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
looping, and inputing this info
Discuss looping, and inputing this info in the C Programming forum on Dev Shed. looping, and inputing this info C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 9th, 2003, 09:07 PM
|
|
Junior Member
|
|
Join Date: Feb 2003
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
looping, and inputing this info
IM TRYIING TO ADD THIS INFO TO MY PROGRAM BELOW. (this is just part of it).. HOW DO I DO THIS. IM NEW
i need to calcule for tuition..im not good on the , and loops, and using constants.....
its asks for name how many credits....etc. i need to make it print up on the output box as resident or non resident when they push r or n.
also it calculates how much money it cost per credit. for instance a resident student pays $200 per credit and cannot go above $3300 quater.
a non resident pays 400 a credit and cannot go above 5500
full time is 12 credits, and they cant go above 20 credits
so it needs to calculate their costs depending on how many credits they enter...
cout << "Enter the first name: ";
cin>> sFirst_name;
cin.ignore( 10000, '\n');
cout << "Enter the last name: ";
cin>> sLast_name;
cin.ignore( 10000, '\n');
cout << "Are you a resident? (Y/N) ";
cin>> Resident;
cin.ignore( 10000, '\n');
cout << "How many credits are you taking: ";
cin>> iCredits;
cin.ignore( 10000, '\n');
{
((iCredits <0) || (iCredits >20));
cout << "Error: done"<<endl;
}
{
if ((Resident == 'R') || (Resident == 'r'));
Res_Tuition = ((iCredits) * (Res_Cred));
cout << "tuition =" << Res_Tuition << endl;
}
if((Resident == 'N') || (Resident =='n'));
{
Non_Res_Tuition = ((iCredits) * (Non_Res_Cred));
cout << "tuition =" << Non_Res_Tuition << endl;
}
|

February 10th, 2003, 05:14 PM
|
 |
Contributing User
|
|
Join Date: Jun 2000
Location: Southern California
Posts: 73
Time spent in forums: 56 m 9 sec
Reputation Power: 13
|
|
How about this:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;
// CPC = Cost Per Credit
const int CPC_NONRES = 400;
const int CPC_RES = 200;
// max tuition
const int MAX_NONRES = 5500;
const int MAX_RES = 3300;
template<class T> inline T MIN(T a, T b) { return (a < b) ? a : b; }
template<class T> inline T MAX(T a, T b) { return (a > b) ? a : b; }
// -----------------------------------------------------------------
string prompt(const string& s)
{
string ans;
while (true) {
cout << s;
cin >> ans;
if (cin.bad() || cin.fail()) {
cout << "** Input error. Try again." << endl << endl;
continue;
}
if (!ans.length())
break;
cin.ignore(256, '\n');
break;
}
return ans;
}
// -----------------------------------------------------------------
int main()
{
string fname, lname, is_res, ncredits;
int credits = 0;
int cpc = CPC_NONRES;
int max = MAX_NONRES;
fname = prompt("\nEnter your first name: ");
lname = prompt("Enter your last name: ");
is_res = prompt("Are you a resident? (y/n): ");
while (true) {
ncredits = prompt("Number of credits: ");
credits = atoi(ncredits.c_str());
if (credits > 0 && credits <= 20)
break;
cout << "** Invalid answer. Must be 1-20." << endl << endl;
}
if (is_res.at(0) == 'Y' || is_res.at(0) == 'y') {
cpc = CPC_RES;
max = MAX_RES;
}
char* total;
char* cost;
sprintf(cost, "$%.2f", (float)cpc);
sprintf(total, "$%.2f", (float)MIN<int>((credits * cpc), max));
printf("\nTuition for %s %s:\n\n", fname.c_str(), lname.c_str());
printf(" CREDITS | PER CREDIT | TOTAL\n");
printf("-------------+----------------+-----------\n");
printf(" %7d | %10s | %8s\n\n", credits, cost, total);
return EXIT_SUCCESS;
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|