Writing a deposit method in C++, Having trouble [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I'm writing a method that accepts the value of amount to be deposited, as a parameter. If the amount is greater than bal (balance) the account will be updated with the new amount. Otherwise it returns the old bal and exits. Here is my code:
double withdraw(double amount)
{
if((bal-amount)<0)
{
throw new Exception
("There were insufficient funds");
else
bal=bal-amount;
return bal;
}
}
I'm having errors with the Exception and the else statement.

You have placed the { } in the wrong place.
double withdraw(double amount)
{
if((bal-amount)<0)
{
throw new Exception("There were insufficient funds");
}
else
{
bal=bal-amount;
}
return bal;
}

Related

c++ exception : throwing std::exception [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I need to throw std::exeption if rad is negetive number, how can I throw?
void Circle::setRad(double rad) {
if (rad < 0)
{
throw(std::exception );
}
radius = rad;
}
Usually you should throw one of the derived classes:
throw std::invalid_argument("radius must be nonnegative");

Segfault on releasing resources [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I keep getting a seg fault on the delete trainArray in my code. I'm trying to get all the pointers to the Train objects and delete them. What am I doing wrong here?
pthread_t tidArray[NUM_TRAINS];
Train* trainArray[NUM_TRAINS];
for (int i = 0; i < NUM_TRAINS; i++){
trainArray[i] = new Train(TRAIN_NAME_ARRAY[i],TRAIN_TRANSIT_TIME_MSECS[i]);
}
int trainInd = 0;
for(trainInd = 0;trainInd<NUM_NORTHERNLY_TRAINS;trainInd++){
pthread_create(&tidArray[trainInd],NULL,initiallyNorthernly,(void*)trainArray[trainInd]);
for(trainInd = NUM_SOUTHERNLY_TRAINS;trainInd<NUM_TRAINS;trainInd++){
pthread_create(&tidArray[trainInd],NULL,initiallySouthernly,(void*)trainArray[trainInd]);
}
for (int i = 0; i < NUM_TRAINS; i++){
trainArray[i] = NULL;
pthread_join(tidArray[i],(void**)&trainInd);
delete trainArray[NUM_TRAINS];
}
return(EXIT_SUCCESS);
}
When you wrote this:
delete trainArray[NUM_TRAINS];
I'm pretty sure you meant this:
delete trainArray[i];
…especially seeing as the first one refers to an array element that does not exist.
I suggest you use a nice std::vector instead so that you can't make silly mistakes like this.

warning C4700: uninitialized local variable 'p' used [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
This code compiles and runs though gives a Microsoft compiler error that I cant fix
warning C4700: uninitialized local variable '' used.
This is in the starting line of the code, I think
void employee::loginemployee()
{
char uname[15];
char pass[15];
char p;
int i=0;
cout<<"\n\t\tEnter User Name :-";
cin>>uname;
puts("\n\t\tEnter Password :-");
while(p!=13)
{
p=_getch();
_putch('*');
pass[i]=p;
i++;
}
pass[i]='\0';
ifstream objdata;
objdata.open("HRStaff",ios::in|ios::out|ios::binary|ios::app);
if(!objdata)
{
cout<<"\n-----Cannot Open the File-----\n";
//return 1;
}
int nflag=0;
while(!objdata.eof())
{
objdata.read((char *)& info, sizeof(info));
if(strcmp(uname,info.uname)==0 )
{
system("cls");
cout<<"\n\n\n\t\t****************************************";
cout<<"\t\t\t\t\t\t\t Welcome TO EMS"<<info.uname<<endl;
cout<<"\t\t****************************************\n"<<endl;
info.putdata("SPS");
cout<<"\n\tPress any key to log out...";
nflag=1;
}
}
if(nflag==0)
{
cout<<"\n\nSorry !! Your Username & Password do not match.";
_getch();
logoutAll();
}
objdata.close();
}
The warning is quite clear. You declare a variable without initialising it:
char p;
then use its uninitialised value:
while(p!=13)
{
// ...
}
Either initialise it before use:
char p = 0; // or any value other than 13
or restructure the logic so its value isn't used until you've assigned to it:
do
{
// ...
} while (p != 13);
Then learn about buffer overflow and stop reading user input into fixed-sized buffers without checking the length. This is C++, not C, so you should usually use std::string to store string values.

What is wrong with this code in string reversal? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
In the following piece of code I get the error mentioned below. Please tell me
Why *p=t gives error here
void reverse (char *p)
{
int length=strlen (p);
int c=0, i=length/2;
char *Temp=p+length-1, t;
while (c<length)
{
t=*Temp;
*Temp=*p
*p=t;
//Gives error as illegal, right operand has type char*
//Why is the error in the above line?
c++;
Temp--;
}
}
There is a semi-colon missing:
t=*Temp;
*Temp=*p ; //--here
*p=t;

Can't return nullpointer in Cpp function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am not able to return the nullptr at the end of this method? Is there some kind of library I need to import?
const char* strstr(const char* string1, const char* string2) {
// TODO:
for (int i = 0; i < strlen(string1); i++) {
for (int j = 0; j < strlen(string2); j++) {
if (string1[i] == string2[i]) {
return &string1[i];
}
}
}
return nullptr;
}
nullptr is a feature introduced in c++11, see: What exactly is nullptr?. You'll need a compiler that supports at least some features of c++11.