So im trying to run a program that receives a set data from a file that updates each time the program is run and new data is added. But when i try to run it the program terminates after trying to receive input from user.The full code is as follow, im using devc++ if that matters
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
struct Deets
{
string NameSite[];
string AddressName[];
int numVaccine[];
int numStall[];
string DateVax[];
string ContNum[];
};
void Outputmenu();
int AddData(int,struct Deets V);
void EditData(int,struct Deets V);
void ShowExisting(int,struct Deets V);
void ClearArr(int,struct Deets V);
int main()
{
int i;
ifstream r("Total.txt");
r>>i;
r.close();
Deets V;
ifstream a;
a.open("Data.txt");
for(int c=0;c<=i;c++)
while(a>>V.NameSite[c]>>V.AddressName[c]>>V.numVaccine[c]>>V.numStall[c]>>V.DateVax[c]>>V.ContNum[c]);
a.close();
int menuS;
do
{
Outputmenu();
cout<<"\nPlease choose from 1-4, press 0 to close menu"<<endl;
cin>>menuS;
if (menuS==1)
AddData(i,V);
else if (menuS==2)
EditData(i,V);
else if (menuS==3)
ShowExisting(i,V);
else if (menuS==0)
cout<<"Thank you for your cooperation, We hope to see you soon! \nClosing program..,";
else
cout<<"Error, not within selection"<<endl;
}
while (menuS!=0);
ofstream b("Data.txt");
for(int c=0;c<=i;c++)
while(b<<V.NameSite[c]<<" "<<V.AddressName[c]<<" "<<V.numVaccine[c]<<" "<<V.numStall[c]<<" "<<V.DateVax[c]<<" "<<V.ContNum[c]<<endl);
return 0;
}
void Outputmenu() //instruction about the command for the user
{
cout << " -hello there!- " << endl;
cout << "===================================================================" << endl;
cout << " ***PLEASE CHOOSE ONE OF THE OPTION BELOW*** " << endl;
cout << "===================================================================" << endl;
cout << "\t1. Add Data (about Vaccination Center)" << endl;
cout << "\t2. Edit Data" << endl;
cout << "\t3. Show Existing Data" << endl;
cout << "\t0. Close Program" << endl;
cout << "===================================================================" << endl;
}
int AddData(int c, struct Deets A)
{
bool rq;
int i = c;
cout<<"Enter your company name/ institute name : ";
cin.ignore();
getline(cin,A.NameSite[i]);
cout<<"\n\t\t Safety Measure"<<endl;
cout<<"-Maintain a distance of 1 meter between one other access to vaccine doses.\n"<<"-Have a dedicated toilets for patient\n"<<"-Ability to maintain room temperature between 19 – 25 degrees \n"<<"-Have an adequate place for patients to wait "<<"\n-Have adequate sharps disposal bins, appropriate for the volume of patients, and securely placed and spaced to mitigate the risk of needle stick injuries."<<endl;
cout<<"-Adequate handwashing facilities for staff, and antimicrobial hand sanitizers available.\n"<<"-Store and handle COVID-19 vaccines under proper conditions, including maintaining cold chain conditions and chain of custody at all times in accordance with a EUA or vaccine package insert, manufacturer guidance, and CDC guidance in the Vaccine Storage and Handling Toolkit."<<endl;
cout<<"-Appropriate security provisions to ensure no unauthorized access to vaccine doses."<<endl;
cout<<"\nAre all requirement met? \n1:Yes \n0:No"<<endl;
cin>>rq;
if(rq == 1)
{
cout<<"\nWhere is the location : "<<endl;
cin>>A.AddressName[i];
cout<<"How many vaccines required : "<<endl;
cin>>A.numVaccine[i];
cout<<"How many vaccine stall can a site hold : "<<endl;
cin>>A.numStall[i];
cin.ignore();
cout<<"Date for vaccine to be distributed"<<endl;
getline(cin,A.DateVax[i]);
cin.ignore();
cout<<"Enter your management contact number : "<<endl;
getline(cin,A.ContNum[i]);
cout<<"Certificate for "<<A.NameSite[i]<<endl;
i++;
}
else
{
cout<<"Thanks for the thought. We hope to cooperate with "<<(A.NameSite[i])<<" in the near future\n\n"<<endl;
if(i>0)
{
i = i-1;
}
else
{
i = 0;
}
}
return i;
}
void EditData(int i,struct Deets A)
{
int c,EN,edit=0;
for (int c=0;c<i;c++) //showing all Company/Institute name data
{
cout << c+1 << ". "<<A.NameSite[c]<<endl;
}
cout << "Enter number you would like to edit : "; //asking user to enter which data user want to edit
cin >> EN; //user enter which data they want to edit
c=EN-1;
cout << A.NameSite[c] << "\n" << "Do you want to edit this data? Yes:1 No:0"<<endl; //asking user whether they want to edit Company/Institute name
cin >> edit;
if(edit ==1) //if true, it will change the data
cin >> A.NameSite[c];
cout <<"Total stall : "<< A.numStall[c] << "\n" << "Do you want to edit this data? Yes:1 No:0"<<endl; //asking user whether they want to edit total Company/Institute vaccine stall
cin >> edit;
if(edit ==1) //if true, it will change the data
cin >> A.numStall[c];
cout <<"Adress : " << A.AddressName[c] << "\n" << "Do you want to edit this data? Yes:1 No:0"<<endl; //asking user whether they want to edit Company/Institute's address
cin >> edit;
if(edit ==1) //if true, it will change the data
cin >> A.AddressName[c];
cout << "Contact number : " << A.ContNum[c] << "\n" << "Do you want to edit this data? Yes:1 No:0"<<endl; //asking user whether they want to edit Company/Institute's contact number
cin >> edit;
if(edit ==1) //if true, it will change the data
cin >> A.ContNum[c];
cout << "Total Vaccine : "<< A.numVaccine[c] << "\n" << "Do you want to edit this data? Yes:1 No:0"<<endl; //asking user whether they want to edit total Company/Institute vaccine needed
cin >> edit;
if(edit ==1) //if true, it will change the data
cin >> A.numVaccine[c];
cout << "Date of vaccine : "<< A.DateVax[c]<< "\n" << "Do you want to edit this data? Yes:1 No:0"<<endl; //asking user whether they want to edit Company/Institute's receiving vaccine date
cin >> edit;
if(edit ==1) //if true, it will change the data
cin >> A.DateVax[c];
}
void ShowExisting(int i, struct Deets A) // void of ShowExisting that show the existing data stored in the program
{
for(int c=0; c<i; c++) //array to show all the existing data in the program so far
{
cout << (c+1) << ". "<< endl; //number that show the count of which set of data shown
cout << "Site Name: " << A.NameSite[c] << endl; //show the name of site in appropriate array of turn c
cout << "Address: " << A.AddressName[c] << endl; //show the address of site in appropriate array of turn c
cout << "Amount Vaccine: " << A.numVaccine[c]<< endl; //show the amount of vaccine needed in site in appropriate array of turn c
cout << "Amount Stall: " << A.numStall[c] << endl; //show the amount of stall needed in appropriate array of turn c
cout << "Vaccination Date: " << A.DateVax[c] << endl; //show the vaccination date in appropriate array of turn c
cout << "Contact Num.: " << A.ContNum[c] << "\n" << endl; //show the contact number of the reprsentative from the site in appropriate array of turn c
}
}
The program runs until input when there is no data in the file but when there is data, the program wont even go past
for(int c=0;c<=i;c++)
while(a>>V.NameSite[c]>>V.AddressName[c]>>V.numVaccine[c]>>V.numStall[c]>>V.DateVax[c]>>V.ContNum[c]);
please help as my caffeine brain is fried
*After considering everyone's response ive decided to rework the basis of the program with what everyone taught, Thank you for those who responded
IMHO, I would rewrite/redesign your code rather than debug it.
Let's start by differentiating a single Deet from a container of Deet:
struct Deet
{
string NameSite;
string AddressName;
int numVaccine;
int numStall;
string DateVax;
string ContNum;
};
You can then overload operator>>:
struct Deet
{
//...
friend std::istream& operator>>(std::istream& input, Deet& d);
};
std::istream& operator>>(std::istream& input, Deet& d)
{
std::getline(input, d.NameSite);
std::getline(input, d.AddressName);
input >> d.numVaccine;
input >> d.numStall;
std::getline(input, d.DateVax);
std::getline(input, d.ContNum);
return input;
}
You could declare a container and input the container like so:
std::vector<Deet> deet_container;
Deet d;
while (input_file >> d)
{
deet_container.push_back(d);
}
BTW, you should always put descriptive variables inside your function declarations. This gives people a better understanding of what the variables are and their positions.
Your function declarations could change:
typedef std::vector<Deet> Deet_Vector;
int AddData(int, Deet_Vector& V);
void EditData(int, Deet_Vector& V);
void ShowExisting(int, Deet_Vector& V);
void ClearArr(int, Deet_Vector& V);
By looking at AddData, the usage of the int first parameter is ambiguous.
Are you adding an int to the container?
Is the int the quantities of items to add to the container?
Likewise with the other functions.
Next, I would add one function at a time to the program, compile and debug, then add another, repeat.
BTW, you can simplify your Outputmenu function:
void Outputmenu()
{
static const char menu_text[] =
" -hello there!-\n"
"===========================================================\n"
" ***PLEASE CHOOSE ONE OF THE OPTION BELOW***\n"
"===========================================================\n"
"\t1. Add Data (about Vaccination Center)\n"
"\t2. Edit Data\n"
"\t3. Show Existing Data\n"
"\t0. Close Program\n"
"============================================================\n"
;
std::cout.write(menu_text, sizeof(menu_text);
}
By using the write method, the text is written as a block and is usually a lot faster than using a lot of operator<<.
Related
So I have to create an AI program than interacts with the user and responds based on the user input. I'm not very experienced, and this has already took hours lmao, I've looked online but I figured I'd actually post my code and try get some help/advice.
Basically the AI helps with maths, I have the program introducing itself and asking what it wants help with but when I enter Addition, Subtraction etc it just responds with numbers when it should respond with "Great, I'll help you with Addition!/(whatever user input)"
Screenshot of first running program: http://prntscr.com/elw7b4
Screenshot after entering what user needs help with: http://prntscr.com/elw7ky
(Obviously it's a bit all over the place at the moment, I did the calculator before anything else hence why it's giving additional results.
The calculator was working before entering the following code: (As you can see http:// prntscr.com /elwavs only two links cos haven't got more than 10 rep)
void Inpsum()
{
cout << "Hello, my name is Eva! I am able to help you with basic Maths! How may I be of Assistance today?" << endl;
float inpsum;
cin >> inpsum;
cout << "Great!, I will help you with " << (inpsum) << endl;
}
but entering the above code broke the calculator.
here is the full code:
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
using namespace std;
//user inputs what he needs help with/program output
void Inpsum()
{
cout << "Hello, my name is Eva! I am able to help you with basic Maths! How may I be of Assistance today?" << endl;
cin >> inpsum;
cout << "Great!, I will help you with " << (inpsum) << endl;
}
//addition function
void Add() {
float add1, add2;
cout << "Please enter two values you want added together" << endl;
cin >> add1;
cin >> add2;
cout << "The answer is: " << (add1 + add2) << endl;
}
//subtraction function
void Subt() {
float subt1, subt2;
cout << "Please enter two values you want subtracted" << endl;
cin >> subt1;
cin >> subt2;
cout << "The answer is: " << (subt1 - subt2) << endl;
}
//division function
void Div()
{
float div1, div2;
cout << "Please enter two values you want divided" << endl;
cin >> div1;
cin >> div2;
cout << "The answer is: " << (div1 / div2) << endl;
}
//multiplication function
void Mult() {
float mult1, mult2;
cout << "Please enter two values you want multiplacted" << endl;
cin >> mult1;
cin >> mult2;
cout << "The answer is: " << (mult1 * mult2) << endl;
}
int main()
{
Inpsum(); //user inputs what they want help with
Add();
Subt();
Div();
Mult();
return 0 ;
}
Basically - I've set the calculator up, and it was working. But upon trying to implement input and output between the user and the program I'm going wrong and have broken everything. Instead of the program saying "Great I'll help you with Addition", it says "Great, I'll help you with -134567432"
I'm not asking for anyone to do it for me, rather point me in the right direction so I can actually know what to do in the future.
Notice you define inpsum using float inpsum;, but what you are trying to store is string, or words. They are not compatible. You can learn something more about data types and strings in C++.
It might help to use an enum since you only have a handful of choices. You could do something like this:
enum class OPERATION : char {
Addition = 'A',
Subtraction = 'S',
Division = 'D',
Multiplication = 'M'
};
Then instead you cin to a string and have the following:
std::string input;
std::cin >> input;
switch(static_cast<OPERATION>(input[0])) {
case OPERATION::Addition:
Add();
break;
case OPERATION::Subtraction:
Subt();
break;
case OPERATION::Division:
Div();
break;
case OPERATION::Multiplication:
Mult();
break;
default:
std::cerr << "Invalid input" << std::endl;
exit(1);
}
Defining the enum will allow you to cast values to it which match its values. This allows you to safely do a switch with defined inputs that you expect to see as your program runs.
I just started learning files and I understand how to set it up and get it to work. I have to write this program where I have to allow the user to enter some information and have the user also update and adjust any data, using binary.
So I can write up until the point where the user can write to and read from the file. But I don't know how to let the user adjust data or add data.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class client {
public:
string name;
int balance;
string id;
};
int main()
{
int ans;
int x;
string nameIn;
string adjName;
client client1;
ofstream out("client1.dat", ios::binary);
cout << "\nDo you want to add information or update info" << endl;
cin >> ans;
if (ans == 1)
{
cout << "\nPlease enter the name of your client" << endl;
cin >> nameIn;
x = nameIn.length();
if (x <= 10)
{
for (int i; i < 10; i++)
{
adjName[i] = nameIn[i];
}
}
else
{
for (int i = x; i < 10; i++)
{
adjName[i] = ' ';
}
}
client1.name = adjName;
cout << "\nPlease enter the balance of your client" << endl;
cin >> client1.balance;
cout << "\nPlease enter the id of your client" << endl;
cin >> client1.id;
cout << "\nThe name of your client is " << endl << client1.name
<< endl << "\nThe balance of your client is " << endl
<< client1.balance << endl << "\nThe id of your client is "
<< endl << client1.id;
out.write(reinterpret_cast<const char*> (&client1), sizeof(client));
}
/*
else if (ans == 2)
{
string answer, newName,line;
cout << "\nWhat name do you want to update? " << endl;
cin >> answer;
cout << "\nWhat is the new name?" << endl;
cin >> newName;
if (out)
}
*/
system("pause");
return 0;
}
so the name needs to be only 10 characters long, so that we can adjust/update it. It compiles and runs, but every time the compiler gets to the part where it checks the name length, it freaks out and says "debug assertion failed"
string subscript out of range.
Also a thing about this code-- if i run it without the bits where you adjust the name to a certain array length, the program runs, and stores everything nicely. But when I try to read back the .dat, it reads it back but exits with an access violation, forcing me to manually stop the debugging. What am I doing wrong?
this is the code for reading the file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class client {
public:
string name;
int balance;
string id;
};
int main()
{
client client1;
char ans;
cout << "\nDo you want to view the information about your client?"
<< endl;
cin >> ans;
ifstream in("client1.dat", ios::binary);
if (ans == 'y' || ans == 'Y')
{
in.read(reinterpret_cast<char*> (&client1), sizeof(client));
cout << "The name is " << endl << client1.name << endl
<< "The balance is " << endl << client1.balance << endl
<< "The id is " << endl << client1.id << endl;
}
system("pause");
return 0;
}
As for the 1st part:
for (int i; i < 10; i++)
// ^
misses to initialize i to zero. Also what if the input was smaller than 10 characters? You're going to access the std::string out of bounds. You should replace the if/else and loops with simply
adjName = nameIn;
while(adjName.length() <= 10) {
adjName += ' ';
}
to get rid of the debug assertion.
For the 2nd part of the question, as already mentioned in the comments you cannot do this with a structure containing classes like std::string.
The reinterpret_cast<char*> (&client1) just obfuscates that std::string uses a pointer to the dynamically allocated character data internally, and that cannot be restored meaningfully when reading the stored data back later (hence the access violation you get).
A viable way might be to use something like
struct client {
char name[11];
int balance;
char id[5];
};
As I guess you need to do this for a homework exercise, and for this purpose that would probably be sufficient.
But you quickly can see the drawbacks, that the character data needs to be fixed in size and you cannot have arbitrary length strings. I never would use such for production ready code.
Another pitfall (as also mentioned) is, that int isn't represented in the same way (order of bytes used, i.e. endianess) in the same way for different CPU architectures. So the binary file can't be used portably with different computers.
The simplest solution is not to use a binary file, but a text formatted file and overload the std::ostream& operator<<(std::ostream&, const client&) and std::istream& operator>>(std::istream&, client&) output/input operators.
Or use some 3rd party library like boost::serialization or google protocol buffers, that supports de-/serialization to binary files.
So for my project I have to...
Create a structure using the format: struct PERSON{string name; int age; float gpa;};
Ask the user to enter the number of records they would like to enter.
Create a dynamic array p of PERSON type to be able to hold all records in part 2
Read data into array p in part 2
Display array p
However, whenever I run my program and begin to enter my data it won't let me enter more than one set of data and the output seems to make little sense.
Here is my code:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
struct PERSON
{
string name;
int age;
float gpa;
};
PERSON *p;
int main()
{
int count;
cout << "Please enter the number of records you wish to enter: ";
cin >> count;
p = new (nothrow) PERSON[count];
if (p == nullptr)
cout << "Error: memory could not be allocated";
for (int i = 0; i < count; i++)
{
cout << "Enter name, age, and gpa: ";
getline(cin, p[i].name);
cin >> p[i].age;
cin >> p[i].gpa;
cout << endl;
}cout << endl;
cout << "This is the content of array p: " << endl;
cout << right << setw(8) << "NAME" << setw(7) << "AGE" << setw(7) << "GPA" << endl;
cout << "--------------------------" << endl;
for (int i = 0; i < count; i++)
{
cout << p[i].name << " " << p[i].age << " " << p[i].gpa << endl;
}
return 0;
}
Now, this format seemed to work just fine when I was copying data from a file into a dynamic array of structures, but I can't seem to get it to work now that I'm dealing with user input.
Here is what a trial run of the program produces:
Please enter the number of records you wish to enter: 3
Enter name, age, and gpa: Robert 21 2.1 // This is the info I tested
Enter name, age, and gpa:
Enter name, age, and gpa:
This is the content of array p:
NAME AGE GPA
--------------------------
-842150451 -4.31602e+008 //?
-842150451 -4.31602e+008 //?
-842150451 -4.31602e+008 //?
Press any key to continue . . .
I've probably gone over the code two dozen times now and have been searching for an answer for quite a while. Any advice/analysis/comments would be greatly appreciated.
So #AndyG suggested that I change the getline(cin, p[i].name) to cin >> p[i].name and that seemed to clear things up. Thank you!
Sorry to trouble everyone over such a silly muck-up.
The Problem
Your getline(cin, p[i].name) is actually grabbing all your input on the same line, and your later cin statements are going to be mismatched.
std::getline should be reserved for getting an entire line into a character buffer or std::string, i.e., it's not always the appropriate tool for reading in a string.
The Solution
Instead, you simply need to replace getline(cin, p[i].name) with cin >> p[i].name.
Live Demo
Other minor changes
Remove the nullptr check:
if (p == nullptr)
cout << "Error: memory could not be allocated";
Because in C++ you will get an exception thrown if the new operator fails to allocate. Like bku_drytt mentioned, you can wrap this in a try catch block instead, although I'd personally just remove it altogether.
#include <new>
// ...
Person* p = null;
try
{
p = new Person[count];
} catch(std::bad_alloc& ex)
{
cerr << "Unable to allocate memory for Person! Error msg: " << ex.what() << "\n";
return 1;
}
Finally, don't forget to delete[] the memory you allocated for p:
// ...
delete[] p;
return 0;
} //end of int main()
Even though your OS will most definitely clean that memory up for you, you should still ensure you clean it up manually. Good practice at least. Alternatively, you can use a std::array or a std::vector instead, which will still give you random access just like the c-style array of PERSON you already have.
The std::getline() function is "misbehaving" in relation to your intentions because there is (I think) a new line character left in the stream input queue.
You can discard such characters by using the following command cin.sync().
Why does this happen? Because operator>> does not read whitespaces, but std::getline() does, so when the cin >> count; statement is executed and you press enter, the new line character (from pressing enter) is left in the stream input queue, which is automatically read by std::getline() upon execution.
There are other functions that can accomplish this and perhaps someone more informed can tell us which is the best choice.
Here is a fixed version:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
struct Person
{
string name;
int age;
float gpa;
};
int main()
{
cout << "Please enter the number of records you wish to enter: ";
int count;
cin >> count;
Person* p = new Person[ count ];
for ( int i = 0; i < count; i++ )
{
cin.sync(); // added this to discard unused characters from input queue
cout << "Enter name: ";
getline( cin, p[ i ].name );
cout << "Enter age: ";
cin >> p[ i ].age;
cout << "Enter gpa: ";
cin >> p[ i ].gpa;
cout << endl;
}cout << endl;
cout << "This is the content of array p: " << endl;
cout << right << setw( 8 ) << "NAME" << setw( 7 ) << "AGE" << setw( 7 ) << "GPA" << endl;
cout << "--------------------------" << endl;
for ( int i = 0; i < count; i++ )
{
cout << p[ i ].name << " " << p[ i ].age << " " << p[ i ].gpa << endl;
}
return 0;
}
Notes:
You do not need to check if Person* p is nullptr because the new operator will throw a std::bad_alloc exception if it fails to allocate enough memory. Consider wrapping that memory allocation in a try catch block if you really want to.
I apologize if this is in the wrong place. I didn't really see an area where to post specific code problems.
I'm tasked by my professor to create a program that allows a user to insert, edit and print out a database of clients. I'm having a problem with one of my functions, it doesn't seem to be comparing something well. The function is the EditClient function on line 85, the logic problem I'm having is on line 93. I included a few tests in the code that makes me certain it is line 93, for example in the else statement on line 99 I have it printing out the same arguments being used in the comparison. Which works! I'm clueless as to why it's not understanding the comparison.
#include <iostream>
#include <iomanip>
using namespace std;
struct ClientInfo{ // the structure template for the client info
char name[40]; //client name
char state[40]; //client state
char city[40];
int PhoneNumber;
long double AccountBalance; //accounts balance
char PaymentDate; //payment date
};
ClientInfo ClientList[10]; //intializes an array with the data type clientinfo
void MainMenu();
void NewClient(ClientInfo List[]); // prototypes
void ViewClient(ClientInfo List[]);
void EditClient(ClientInfo List[]);
int main(){
MainMenu();
system("pause");
}
void MainMenu(){ //this function is the main menu function
char choice = 4;
bool loop = true;
while (loop){
cout << "Welcome to the client database, enter 1 to view the clients, 2 to edit a client , and 3 to enter an entire new client. 0 to quit" << endl; //main menu prompt
cin >> choice;
if (choice == '1'){
ViewClient(ClientList);
}
else if (choice == '2'){
EditClient(ClientList);
}
else if (choice == '3'){
NewClient(ClientList);
}
else if (choice == '0'){
cout << "thank you for using the client database, closing out now" << endl;
loop = false;
}
else{
cout << "invalid number" << endl;
}
}
}
void NewClient(ClientInfo List[]){//function that goes through cins to insert client data
int desiredTimes = 0; // the number of clients the person wish to enter
cout << "how many clients are you entering ?, your list current has "<<endl;
cin >> desiredTimes;
cout << "entering new client function..." << endl;
for (int cnt = 0; cnt < desiredTimes; cnt++){ // runs the program exactly the amount of times the person wished.
cout << "please enter client name" << endl;
cin.ignore();
cin.getline(List[cnt].name, 40);
cout << "please enter client state" << endl; // the getline is used here because there may be spacings within these first 3 catagories
cin.getline(List[cnt].state, 40);
cout << "please enter client city" << endl;
cin.getline(List[cnt].city, 40);
cout << "please enter client Phone Number" << endl;
cin.ignore(); // this is used to prevent the getline from causing issues with the cin
cin >> List[cnt].PhoneNumber;
cout << "please enter client Account Balance" << endl;
cin >> List[cnt].AccountBalance;
cout << "please enter client Payment Date" << endl;
cin >> List[cnt].PaymentDate;
}
}
void EditClient(ClientInfo List[]){ // function to search for a name requested and display the info
char name[40];
cout << "what is the name of the client you wish to view (be specific)?";
cin >> name;
bool loop = true; // boolean for the loop
int cnt = 0; // position in the array
while (loop){
if (cnt < 11){
if (name == List[cnt].name){ //if true, prints out the client's info
cout << "true";
/*NewClient(List[cnt]);*/
loop = false; // ends the loop
}
else{
cout << name << " " << List[cnt].name << endl;
cnt++;
}
}
else{
cout << "your client isn't in the database M8" << endl;
loop = false;
}
}
}
void ViewClient(ClientInfo List[]){//this function goes through the client list and displays a particular client
cout << "the following is a huge overrun of all the data inside this list, prepare your self." << endl;
for (int cnt = 0; cnt <= 10; cnt++){//goes through until the counter is greater than the size of the list in the parameter
cout << endl;
cout << List[cnt].name;
cout << List[cnt].state;
cout << List[cnt].city;
cout << List[cnt].PhoneNumber;
cout << List[cnt].AccountBalance;
cout << List[cnt].PaymentDate;
}
}
this is the specific line, where i'm having a comparision error.
if (name == List[cnt].name){ //if true, prints out the client's info
if (name == List[cnt].name) is doing a pointer comparison, not a string comparison. This will only be true if name and List[cnt].name point to the same memory location, which they never will. You should probably be using std::string instead of char[], but if you need to use char[] for some reason, you'll need to use the strcmp() function to compare them.
instead of the if statement you are using, trying using this statement,
if (strcmp(name,List[cnt].name)==0){
It should work properly :)
I'm new to programming and am trying to figure out why this is happening.
Basically I'm asked to design a menu driven program that does some basic deposit/withdrawal calculations. I'm supposed to write different functions for each process and call on them when necessary.
I'm having 2 problems:
1) My functions aren't updating my variables within the program. For example it will run, I can enter my starting balance, I can enter my transaction type and amount but every time I switch from deposit to writing a check, it resets the balance to the original user input.
2) When I want the program to exit, it is still asking me for the double input. Not sure how to make it accept just an "E" instead of "E number"
Thanks in advance.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Prototypes for my functions that will be called in
void displayMenu();
double checkProcess(double, double);
double depositProcess(double, double);
double totalServCharge(double);
int main()
{
//Variables needed for the problem
char choice; //menu choice
double transAmt, balance, numbServCharge; //transaction amount, current balance, total service charges, number of service charges
numbServCharge = 0; //Start the counter for number of service charges at zero
cout << "Checkbook Program\n\n";
cout << "Enter the beginning balance: ";
cin >> balance; //get the initial balance
cout << endl;
do
{
//Call the display menu function
displayMenu();
//Get user's choice and transaction amount from menu
cout << "Enter a transaction: ";
cin >> choice >> transAmt;
choice = toupper(choice);
cout << endl;
//Create an error message for invalid choice and get a second choice
while((choice != 'C') && (choice != 'D') && (choice != 'E'))
{
cout << "Invalid selection. Please choose C, D or E: ";
cin >> choice;
}
//Set up for option #1 -- using a check
if(choice=='C')
{
//Call check function and service charges function
checkProcess(transAmt, balance);
totalServCharge(numbServCharge);
}
//Set up for option #2 -- deposits
if(choice=='D')
{
//Call deposit function and service charges function
depositProcess(transAmt, balance);
totalServCharge(numbServCharge);
}
}while(choice != 'E'); //Close program if option 3 is selected
//Display final balance
cout << "Processing end of the month";
cout << "\nFinal balance : $ " << fixed << setprecision(2) << balance << endl << endl;
system("pause"); //Pause screen so it doesn't close right away
return 0;
}
void displayMenu()
{
//Highlight menu options
cout << "\nCommands\n";
cout << "C amount - process a check in a specific amount\n";
cout << "D amount - process a deposit in a specific amount\n";
cout << "E - end the program\n\n";
}
double checkProcess(double transAmt, double balance)
{
cout << "\nProcessing check for $" << fixed << setprecision(2) << transAmt;
transAmt = transAmt + .25; //Add the service charge onto the transaction
balance = balance - transAmt; //Update account balence
cout << "\nBalance: $" << fixed << setprecision(2) << balance;
cout << "\nService charge: $0.25 for a check\n";
return balance;
}
double depositProcess(double transAmt, double balance)
{
cout << "\nProcessing Deposit for $" << fixed << setprecision(2) << transAmt << endl;
transAmt = transAmt - 0.25; //Add the service charge onto the deposit
balance = balance + transAmt; //Update account balence
cout << "Balance: $" << fixed << setprecision(2) << balance << endl;
return balance;
}
double totalServCharge(double numbServCharge)
{
double totalServCharge = 0;
numbServCharge++; //Add one to the number of service charges there have been
totalServCharge = .25 * numbServCharge; //Update total cost of service charges
cout << "Total service charges: $" << fixed << setprecision(2) << totalServCharge << endl; //Tell user total service charges so far
return numbServCharge;
}
(1) Your variables aren't updating because you aren't changing them; you're changing the copies passed to your subroutines. You either need to pass the current balance by reference (so that when you change it in the subroutine scope, you're also changing the original) or assign the return value from your subroutines to your current balance.
When you pass a variable by value (as you are now), the program makes a copy of that variable for use in the function scope. When you leave that scope, the copy is deleted. Do this when you want to be sure a subroutine won't alter anything in the scope from which it is called. If you want the subroutine to alter the variables you pass to it, pass the variables by reference. In this function signature, "transAmt" is passed by value and "balance" is passed by reference:
double foo(double transAmt, double& balance);
Incidentally, if you pass the balance by reference there's no reason to return it. You aren't doing anything with the returned value right now anyway.
(2) If you want to not ask for a number when "E" is given, make separate "cin" statements for the number in the conditional blocks for "C" and "D". Right now all your input code is shared between all cases. It would be even better if you arrange things so that you exit the loop before a number is asked for. This way, you only have to write that "cin" statement once.
It's becouse in C++ when you call a function with parameter, you only operate on COPIES of those parameters. If you want to change the original value, pass variable as reference.
See those simple examples:
#include <iostream>
using namespace std;
void normal(int a)
{
a += 10;
}
void by_reference(int &a) //Notice ampersand
{
a += 10;
}
int by_return(int a)
{
return a + 10;
}
int main()
{
int a = 5;
cout << "Start: " << a <<endl;
normal(a);
cout << "Normal: " << a <<endl;
by_reference(a);
cout << "Reference: " << a <<endl;
a = by_return(a);
cout << "Return: " << a <<endl;
}
Now you can see that if you pass the variable as reference it gets changed.
Even if just putting the & before parameter names would fix your problem, I'd recommend you using a return, it generally is simplier and makes your code look better (it's much easier to understand).
Also, correct your indentation. It's very bery important when you work with bigger projects.
Hope this helps. :)
1 | The first problem is occurring because you are not actually modifying the user's input. You are passing the variable in but once the new value is returned you are not storing it, thus the value is forgotten in the main function. Not the best solution, but it might suit you to do this:
if (choice == 'D')
{
//Call deposit function and service charges function
balance = depositProcess(transAmt, balance); // Overwrite balance
numbServCharge = totalServCharge(numbServCharge); // Overwrite numbServCharge
}
Or in a more efficient way, you can pass in the variable by reference. All you would need to do is change the function prototype.
void totalServCharge(double&);
void depositProcess(double&, double&);
Now when the variable is passed in, it will be modified directly by the function. Also note the function data type changed to void because you no longer need to return a value from the function.
2 | Your second problem is the way you handle user input. If you want to ask for multiple input, it might sometimes be best to do it with separate cout and cin statements.
The way it is set in your program is for the user to enter two inputs at once, therefore if you only enter E, it waits patiently as it should for your next input (transAmt). It might be better to process the action desired and then have an amount entered afterwards.
Very general example:
do {
cout << "1 : Transaction or 2 : Exit" << endl;
cin >> choice;
if (choice == 2)
break;
cout << "1. Deposit\n" << "2. Check" << endl;
cout << "Enter transaction type followed by amount" << endl;
cin >> transType >> amount;
// Then do what was specified in choice
} while (true); // Or whatever condition pleases you