PDA

View Full Version : C PLUS PLUS help!



cheer4blue24
04-18-2006, 04:03 PM
ok this thread is in honor of bbomber72000. Ok I need help with a bank program. I'll give you details later about the program.

Mr. Wertz
04-18-2006, 04:15 PM
Its actually an ATM program we are working on, but whatever.

<\randomness>

Leeza
04-18-2006, 05:38 PM
WTH? Do not use this forum for spam. Take it to PM or IM because you obviously aren't looking for any help in here.

I will reopen this, but if one more spam post is posted by either of you, the thread will get closed again.

Cruise Control
04-18-2006, 06:48 PM
In order to get help, you need to post the program prompt, and say what part you are stuck on.

BTW, get a new prompt if you lost it.

cheer4blue24
04-20-2006, 04:19 PM
ok I need help on my next program.

The prompt goes like this:

Write a multi function program that will read the names of students and their respective scores for a quiz from a data file(NAME_QUIZ.DAT). Next, determine which person had the highest score as well as the lowest. Finally print that information to a data file.

We learned about sequential access files, stream operation modes and random access files in class. We also learned about ifstream.h, ofstream.h and infile. Where do I get started in the program?

This is NAME.QUIZ.DAT :
Allen Anderson
72
Bob Bennett
38
Carla Cobb
91
Dave Davis
76
Earl Eaton
92
Frank Floyd
89
Gavin George
77
Helen Hightower
98
Ingrid Ingersolls
90
Jack Johnson
65
Ken Kennedy
97
Louise Larkin
87
Maria Melendez
86
The End
0

Flying Mullet
04-20-2006, 04:24 PM
You will need four local variables, the highest and lowest score, each initialized to 0 and the names of the student's with the highest and lowest scores. Then read through the file and compare the score to the highest existing score. If it's higher, store that student's name and score as the new highest. Do the same with the lowest. Then output the highest and lowest to the output file.

As far as how to develop it, be sure to do it on piece at a time as it's much easier to test and debug. I would reccomend writting the code to read in the file. Then write the code to find the highest score. Then the code for the lowest. Finally, write the code to generate the output.

cheer4blue24
04-20-2006, 04:40 PM
I think I get what you are saying. This is what I have come up with after minutes of thinking. (I'm using Dev C++)

#include &lt;conio.h&gt;
#include &lt;iostream&gt;
#include &lt;iomanip.h&gt;
#include &lt;fstream.h&gt;
#include &lt;dos.h&gt;

int main()
{
int highest;
int lowest;
char namehighest;
char namelowest;

highest=0;
lowest=0;

outfile.open("NAME_QUIZ.DAT", ios::out);
}

Flying Mullet
04-20-2006, 05:57 PM
Yeah, that's roughly what I was thinking.

Then write the code to read in the input data and test, then write the code to get the highest test score and test, etc...

Endless
04-20-2006, 10:40 PM
Yeah, that's roughly what I was thinking.

Then write the code to read in the input data and test, then write the code to get the highest test score and test, etc...

You can test both min and max at the same time (while reading, in fact).

Dr Unne
04-20-2006, 11:12 PM
You can test both min and max at the same time (while reading, in fact).

I think he meant "test the code", not "test the data". :)

Flying Mullet
04-21-2006, 12:15 AM
I think he meant "test the code", not "test the data". :)
Yes, incremental development and testing.

Endless
04-21-2006, 09:22 AM
Ah, I see :p That said, you can write both min and max at the same time and test.

Mr. Wertz
04-26-2006, 05:06 PM
OK this is what I got:

# include <conio>
# include <iostream>
# include <iomanip>
# include <dos>

using namespace std;

int main()
{
int score;
char user_name[25];
char user_score[3];
ifstream infile;


infile.open("NAME_QUIZ.DAT", ios::in);



if(infile)
{
infile.get(user_name,25);
infile.ignore(80,'\n');
infile.get(user_score, 3);
infile.ignore(80, '\n');
score = atoi(user_score);

cout << user_name<< endl;
cout <<user_score<<endl;
getch();

}
else
{
cout << "ERROR!\n";
}
if(infile)
{
infile.get(user_name,25);
infile.ignore(80,'\n');
infile.get(user_score, 3);
infile.ignore(80, '\n');
score = atoi(user_score);

cout << user_name<< endl;
cout <<user_score<<endl;
getch();

}
else
{
cout << "ERROR!\n";
}
if(infile)
{
infile.get(user_name,25);
infile.ignore(80,'\n');
infile.get(user_score, 3);
infile.ignore(80, '\n');
score = atoi(user_score);

cout << user_name<< endl;
cout <<user_score<<endl;
getch();

}
else
{
cout << "ERROR!\n";
}
if(infile)
{
infile.get(user_name,25);
infile.ignore(80,'\n');
infile.get(user_score, 3);
infile.ignore(80, '\n');
score = atoi(user_score);

cout << user_name<< endl;
cout <<user_score<<endl;
getch();

}
else
{
cout << "ERROR!\n";
}
if(infile)
{
infile.get(user_name,25);
infile.ignore(80,'\n');
infile.get(user_score, 3);
infile.ignore(80, '\n');
score = atoi(user_score);

cout << user_name<< endl;
cout <<user_score<<endl;
getch();

}
else
{
cout << "ERROR!\n";
}
if(infile)
{
infile.get(user_name,25);
infile.ignore(80,'\n');
infile.get(user_score, 3);
infile.ignore(80, '\n');
score = atoi(user_score);

cout << user_name<< endl;
cout <<user_score<<endl;
getch();

}
else
{
cout << "ERROR!\n";
}
if(infile)
{
infile.get(user_name,25);
infile.ignore(80,'\n');
infile.get(user_score, 3);
infile.ignore(80, '\n');
score = atoi(user_score);

cout << user_name<< endl;
cout <<user_score<<endl;
getch();

}
else
{
cout << "ERROR!\n";
}
if(infile)
{
infile.get(user_name,25);
infile.ignore(80,'\n');
infile.get(user_score, 3);
infile.ignore(80, '\n');
score = atoi(user_score);

cout << user_name<< endl;
cout <<user_score<<endl;
getch();

}
else
{
cout << "ERROR!\n";
}
if(infile)
{
infile.get(user_name,25);
infile.ignore(80,'\n');
infile.get(user_score, 3);
infile.ignore(80, '\n');
score = atoi(user_score);

cout << user_name<< endl;
cout <<user_score<<endl;
getch();

}
else
{
cout << "ERROR!\n";
}
if(infile)
{
infile.get(user_name,25);
infile.ignore(80,'\n');
infile.get(user_score, 3);
infile.ignore(80, '\n');
score = atoi(user_score);

cout << user_name<< endl;
cout <<user_score<<endl;
getch();

}
else
{
cout << "ERROR!\n";
}
if(infile)
{
infile.get(user_name,25);
infile.ignore(80,'\n');
infile.get(user_score, 3);
infile.ignore(80, '\n');
score = atoi(user_score);

cout << user_name<< endl;
cout <<user_score<<endl;
getch();

}
else
{
cout << "ERROR!\n";
}
if(infile)
{
infile.get(user_name,25);
infile.ignore(80,'\n');
infile.get(user_score, 3);
infile.ignore(80, '\n');
score = atoi(user_score);

cout << user_name<< endl;
cout <<user_score<<endl;
getch();

}
else
{
cout << "ERROR!\n";
}
if(infile)
{
infile.get(user_name,25);
infile.ignore(80,'\n');
infile.get(user_score, 3);
infile.ignore(80, '\n');
score = atoi(user_score);

cout << user_name<< endl;
cout <<user_score<<endl;
getch();

}
else
{
cout << "ERROR!\n";
}
if(infile)
{
infile.get(user_name,25);
infile.ignore(80,'\n');
infile.get(user_score, 3);
infile.ignore(80, '\n');
score = atoi(user_score);

cout << user_name<< endl;
cout <<user_score<<endl;
getch();

}
else
{
cout << "ERROR!\n";
}
infile.close();





return 0;
}

I need to know how to make it loop instead of those if statements, and I need it to print the highest and lowest scores.

Flying Mullet
04-26-2006, 05:08 PM
It's been a while since I've written any C++ but I know you can loop in a manner similar to this:


#include<conio>
#include<iostream>
#include<iomanip>
#include<fstream>
#include<dos>
using namespace std;

int main()
{
int score;
char user_name[25];
char user_score[3];
ifstream infile;


infile.open("NAME_QUIZ.DAT", ios::in);

while(infile)
{
infile.get(user_name,25);
infile.ignore(80,'\n');
infile.get(user_score, 3);
infile.ignore(80, '\n');
score = atoi(user_score);

cout << user_name << endl;
cout << user_score << endl;
getch();

}
infile.close();

return 0;
}

Mr. Hid-Man
04-30-2006, 11:22 PM
Ok this is the finished bank program that I attached to the post. It works and does the things I wanted it to:
1. Withdraw from checking acc
2. Withdraw from Savings acc
3. deposit to checkings acc
4. deposit to savings acc
5. see your balance in checkings
6. see your balance in savings
7. quit

I have a pin number that is installed and it is 7410.

Now, I have to make data files of at least 5 customers using sequential access files. I have to put a name, a pin, a checking balance, and a savings balance of the customer. Where do I start.?


//Header files.
#include <conio.h>
#include <iostream>
#include <iomanip>
#include <fstream>

//prints out the textfile.
using namespace std;
ofstream outfile ("AtmUpdate.txt");

int pinnumber = 7410;
double checkbalance = 314.19;
double savebalance = 708.03;

struct ATM
{
char name[41];
int pinnumber2;
int option;
double withdrawlcheckingamount;
double withdrawlsavingsamount;
double depositsavingsamount;
double depositcheckingamount;
double depositsbalance;
double deposit;
double withdrawl;
double endingbalance;
};
ATM record;

void ProgramDescription(); //
int Get_pinnumber(); //
int Get_pinnumber1(); //
int Get_pinnumber2(); //
int Get_option(); //
int withdrawlC(); //
int withdrawlS(); //
int depositC(); //
int depositS(); //
int calcwithc(); //
int calcwiths(); //
int calcdepc(); //
int calcdeps(); //

int main()
{
ATM record;

ProgramDescription();
Get_pinnumber();
Get_option();

outfile.open("BANK.DAT",ios::app);

getch();
return 0;
}

//Program Description.
void ProgramDescription()
{
cout << "This program simulates an ATM machine.\n\n";
cout << "Welcome to Banks-O-Million.\n\n";
cout << "Please hit any key to continue.\n\n";
getch();
}

//Gets PIN number.
int Get_pinnumber()
{
system ("cls");
cout << "Please enter your 4 digit PIN number.\n";
cin >> record.pinnumber2;

if (pinnumber == record.pinnumber2)
{
cout << "You may move on to the next screen. Hit any key to get there." << endl;
getch();
}

else
{
cout << "You have entered the wrong PIN number. You have 2 more attempt(s) left. Please hit any key to get to the next screen and try again." << endl;
getch();
Get_pinnumber1();
}
}

//if they get it wrong, it goes here.
int Get_pinnumber1()
{

cout << "Please enter your 4 digit PIN number.\n";
cin >> record.pinnumber2;

if (record.pinnumber2 == pinnumber)
{
cout << "You may move on to the next screen. Hit any key to get to the next screen." << endl;
getch();
}

else
{
cout << "You have entered the wrong PIN number. You have one more attempt(s) left. Please hit any key to get to the next screen, and try again." << endl;
getch();
Get_pinnumber2();
}
}

//If they get it wrong again, it goes here.
int Get_pinnumber2()
{

cout << "Please enter your 4 digit PIN number.\n";
cin >> record.pinnumber2;

if (record.pinnumber2 == pinnumber)
{
cout << "You have entered the correct PIN number. Please hit any key to get to the next screen." << endl;
}

else
{
cout << "You have entered the wrong PIN number 3 times incorrectly. This ATM is now eating up your card.\a\a\a\a\a\a\a." << endl;
return 0;
}
}

//Gets their option for what they want to do.
int Get_option()
{
system ("cls");
cout << "What would you like to do today: \n";
cout << "Enter an option:\n\n";
cout << "1-Withdraw from your Checking account\n\n";
cout << "2-Withdraw from Savings account.\n\n";
cout << "3-Deposit to Checking account.\n\n";
cout << "4-Deposit to Savings account.\n\n";
cout << "5-See your balance in your checking account.\n\n";
cout << "6-See your balance in your savings account.\n\n";
cout << "7-Quit.\n\n";
cout << "Option: ";
cin >> record.option;
system ("cls");
//prints out atm option chosen.
switch (record.option)
{

//withdrawal from checking option.
case 1:
withdrawlC();
break;

//withdrawl from savings option.
case 2:
withdrawlC();
break;

//Deposit to checking account
case 3:
depositC();
break;

//Deposit to savings account.
case 4:
depositS();
break;

//See checking balance.
case 5:

cout << "You currently have $" << checkbalance << " in your checking account.\n";
cout << "Hit any key to end the program";
return 0;
break;

//See savings balance.
case 6:

cout << "You currently have $" << savebalance << " in your savings account.\n";
cout << "Hit any key to end the program";
return 0;
break;

//Quit.
case 7:
cout << "Thank you for using this ATM. Please hit any key to exit.";
return 0;

//All other cases, invalid

default:
cout << "\n";
cout << record.option;
cout << " is invalid. Please try again.";
Get_option();
}
}

int withdrawlC()
{
cout << "\nHow much do you wish to withdraw? ";
cin >> record.withdrawlcheckingamount;
calcwithc();

return 0;
}

int calcwithc()
{
double checkbalance = 314.19;
double savebalance = 708.03;

record.endingbalance = checkbalance - record.withdrawlcheckingamount;
cout << "\nYour new balance is: $";
cout << record.endingbalance;
}

int withdrawlS()
{
cout << "\nHow much do you wish to withdraw? ";
cin >> record.withdrawlsavingsamount;
calcwiths();
return 0;
}

int calcwiths()
{
double savebalance = 708.03;

record.endingbalance = savebalance - record.withdrawlsavingsamount;
cout << "\nYour new balance is: $";
cout << record.endingbalance;

return 0;
}

int depositC()
{
cout << "\nHow much do you wish to deposit? ";
cin >> record.depositcheckingamount;
calcdepc();
}

int calcdepc()
{
double checkbalance = 314.19;

record.endingbalance = checkbalance + record.depositcheckingamount;
cout << "\nNew balance: $";
cout << record.endingbalance;

return 0;
}

int depositS()
{

cout << "\nHow much do you wish to deposit? ";
cin >> record.depositsavingsamount;
calcdeps();
}

int calcdeps()
{
double savebalance = 708.03;

record.endingbalance = savebalance + record.depositsavingsamount;
cout << "\nNew balance: $";
cout << record.endingbalance;


return 0;
}

Cruise Control
05-06-2006, 04:39 PM
Holy <img src="/xxx.gif"><img src="/xxx.gif"><img src="/xxx.gif"><img src="/xxx.gif">. It's a good thing I'm not in that class. I'd fail.

Mr. Hid-Man
05-09-2006, 04:41 PM
scratch that. I already did the seq files.

Now I need to create a "balance class" for the atm program. This class should receive the current balance for an account(savings or checking) and the amount of the transaction from your program and return the updated balance. This class should be utilized every time a balance changes. To simplify the use of the class you should send negative values to the class for withdrawals and positive values for deposits.

Any suggestions? This is a new type of programming that is getting me thinking a lot.

Flying Mullet
05-09-2006, 04:52 PM
So you would have two methods, one for deposit and one for withdrawl. Is that what you're asking?

Mr. Hid-Man
05-09-2006, 05:12 PM
im asking where do i start to develop my balance class

Mr. Hid-Man
05-12-2006, 04:46 PM
this is what I got. I made a whole different class and he told me it was way too complicated.







#ifndef _BALANCE_H
#define _BALANCE_H

class balance
{
public:
balance();
float getBalance();
float sendBalance();

private:
float returnBalance();

};

Mr. Hid-Man
06-08-2006, 05:32 AM
This is a program that will ask the user to pick a state, capital, date of unionization, and the postal abbreviation. This will use the vector class and I will use a data file. What is wrong with my program? It has no errors, though. When I type a state nothing shows up.




#include <string>
#include <conio.h>
#include <iostream>
#include <fstream>
#include "vector.h"

using namespace std;


struct US
{
char KeyIn[50];
char Abb[50];
char Name[50];
char Capital[50];
char Union[50];
};

vector <US> States(50);

//function declarations
void Intro();
void UserInter(US &a);
void GetData(US &a);
void Result(US &a);

//main functions
int main()
{
US a;

Intro();
UserInter(a);
GetData(a);
Result(a);

getch();
return 0;
}

void Intro()
{
cout << "This is the state program. Enter the postal abbreviation, ";
cout << "capital, state, or the\ndate in which the state entered ";
cout << "the union. ";
cin.get();
system("cls");
}

void UserInter(US &a)
{
system("cls");
//Gets user's search term
cout << "Key in one of the things please. ";
cin.get(a.KeyIn, 50);
cin.ignore(80, '\n');
system("cls");
}

void GetData(US &a)
{

ifstream infile;
infile.open("NAME_QUIZ3.DAT", ios::in);

int i = 0;

while(!infile.eof())
{
//Abbreviation
infile.get(States[i].Abb, 50);
infile.ignore(70, '\n');

//State
infile.get(States[i].Name, 50);
infile.ignore(70, '\n');

//Capital
infile.get(States[i].Capital, 50);
infile.ignore(70, '\n');

//Date
infile.get(States[i].Union, 50);
infile.ignore(70, '\n');


i++;

}
}

void Result(US &a)
{
for(int b = 0; b <= 50; b++)
{
if(strcmp(States[b].Abb, a.KeyIn) == 0)
{
cout << "Postal Abbreviation: " << States[b].Abb << endl;
cout << "State: " << States[b].Name << endl;
cout << "Capital City: " << States[b].Capital << endl;
cout << "Date of Unionization: " << States[b].Union << endl;
getch();
}
else if(strcmp(States[b].Name, a.KeyIn) == 0)
{
cout << "Postal Abbreviation: " << States[b].Abb << endl;
cout << "State: " << States[b].Name << endl;
cout << "Capital City: " << States[b].Capital << endl;
cout << "Date of Unionization: " << States[b].Union << endl;
getch();
}
else if(strcmp(States[b].Union, a.KeyIn) == 0)
{
cout << "Postal Abbreviation: " << States[b].Abb << endl;
cout << "State: " << States[b].Name << endl;
cout << "Capital City: " << States[b].Capital << endl;
cout << "Date of Unionization: " << States[b].Union << endl;
getch();
}
}
}


data file below
AL Alabama Montgomery Dec. 14, 1819
AK Alaska Juneau Jan. 3,1959
AZ Arizona Phoenix Feb. 14, 1912
AR Arkansas Little Rock June 15,1836
CA California Sacramento Sept. 9, 1850
CO Colorado Denver Aug. 1, 1876
CT Connecticut Hartford Jan. 9, 1788
DE Delaware Dover Dec. 7, 1787
FL Florida Tallahassee Mar. 3, 1845
GA Georgia Atlanta Jan. 2, 1788
HI Hawaii Honolulu Aug. 21, 1959
ID Idaho Boise July 3, 1890
IL Illinois Springfield Dec. 3, 1818
IN Indiana Indianapolis Dec. 11, 1816
IA Iowa Des Moines Dec. 28, 1846
KS Kansas Topeka Jan. 29, 1861
KY Kentucky Frankfort June 1, 1792
LA Louisiana Baton Rouge Apr. 30, 1812
ME Maine Augusta Mar. 15, 1820
MD Maryland Annapolis Apr. 28, 1788
MA Massachusetts Boston Feb. 6, 1788
MI Michigan Lansing Jan. 26, 1837
MN Minnesota Saint Paul May 11, 1858
MS Mississippi Jackson Dec. 10, 1817
MO Missouri Jefferson City Aug. 10, 1821
MT Montana Helena Nov. 8, 1889
NE Nebraska Lincoln Mar. 1, 1867
NV Nevada Carson City Oct. 31, 1864
NH New Hampshire Concord June 21, 1788
NJ New Jersey Trenton Dec. 18, 1787
NM New Mexico Santa Fe Jan. 6, 1912
NY New York Albany July 26, 1788
NC North Carolina Raleigh Nov. 21, 1789
ND North Dakota Bismarck Nov. 2, 1889
OH Ohio Columbus Mar. 1, 1803
OK Oklahoma Oklahoma City Nov. 16, 1907
OR Oregon Salem Feb. 14, 1859
PA Pennsylvania Harrisburg Dec. 12, 1787
RI Rhode Island Providence May 29, 1790
SC South Carolina Columbia May 23, 1788
SD South Dakota Pierre Nov. 2, 1889
TN Tennessee Nashville June 1, 1796
TX Texas Austin Dec. 29, 1845
UT Utah Salt Lake City Jan. 4, 1896
VT Vermont Montpelier Mar. 4, 1791
VA Virginia Richmond June 25, 1788
WA Washington Olympia Nov. 11, 1889
WV West Virginia Charleston June 20, 1863
WI Wisconsin Madison May 29, 1848
WY Wyoming Cheyenne July 10, 1890

Dr Unne
06-08-2006, 06:19 AM
Best scenario: Program loudly does the right thing.
OK scenario: Program silently does the right thing.
Bad scenario: Program loudly does the wrong thing.
Very bad scenario: Program silently does the wrong thing and you somehow discover the problem anyways.
Worst scenario: Program silently does the wrong thing and no one figures it out (until the nuclear reactor explodes).

The fourth is what you have, so it could be worse. :) Silent bugs are really really bad though.

If you have a big complex multi-branch conditional like yours in the Result() function, you should put a default else{} catch-all block. Your program should never reach this else{} block unless something goes wrong. So unless you have a good reason to do otherwise, in the else{} block you should make your program print a large nasty error message and die a horrible death. (Have the program exit with a return value indicating failure.) Have the error message print as much relevant information as possible. In this case I'd have it print something like this at the very least:



if(strcmp(States[b].Abb, a.KeyIn) == 0)
{
...
}
else if(strcmp(States[b].Name, a.KeyIn) == 0)
{
...
}
else if(strcmp(States[b].Union, a.KeyIn) == 0)
{
...
}
else
{
cerr << "ERROR: invalid user input: [" << a.KeyIn << "] != [" << States[b].Abb << "] [" << States[b].Name << "] [" << States[b].Union << "]" << endl;
// cleanup here if necessary (close filehandles, cleanly deallocate memory, flush buffers if necceasy etc. etc.)
exit(1);
}

Putting some kind of quoting character( my [] above ) around the output variables will let you know if you're outputting a blank string or a string full of whitespace, which is a hard bug to see visually otherwise. (A good reason to use a monospace font in your console window by the way.)

Another possibility would be to loop and re-prompt for input until you get some that's valid, rather than just having your program kill itself. Or whatever else you'd like.

If you have access to a debugger, you can use that to do the same thing in a perhaps more helpful way. Set a breakpoint somewhere in or before the Result() function, and inspect the values of all your data structures as of that moment.

In general you should put those kind of error checks in every program you write, to cover every possible thing you can imagine that could be broken, and then some. Imagine the file doesn't exist. Imagine the file exists but is full of gibberish. Imagine a directory exists with the same name as the file you want. Imagine the file exists but you have no permissions to read it. Imagine the user enters a single space as his input. Imagine the user somehow manages to enter nothing as the input. Imagine the user enters 1000 characters of gibberish. Imagine the user enters numbers if you're expecting text, or text if you're expecting numbers.

A good program will gracefully handle all of those things. At the very least, complain loudly. At most, die a horrible death. If you build this kind of checking into your program as you write it, you will find that it's very easy to debug, and very easy for users to use (or at least easy for you to fix once the user starts complaining to you that it's broken).

Endless
06-13-2006, 02:35 PM
As it is, your program cannot correctly read from your data file. And all these if are useless, you can do all of them at once (if cond1 or cond2 or cond 3).

Also, using Borland only functions = bleh :p