r/programminghelp • u/deezhuntz • Sep 26 '22
C++ Help with c++ homework
I am trying to complete an assignment for my c++ programming class but the autograder keeps giving me the error message " Test Timed Out, Potential Infinite Loop ".
The assignment is to: Create a program that displays the ending balance in a savings account, given the beginning balance, the deposit amounts, and the withdrawal amounts. Use two loops in the program: one to get the deposit amounts, and the other to get the withdrawal amounts.
The code I have written is :
"#include<iostream>
#include<iomanip>
using namespace std;
int main(){
double beginningBalance;
cout<<"Enter the beginning balance:";
cin>>beginningBalance;
cout<<"Enter the deposit amounts";
double deposit = 0.0;
double endingBalance = beginningBalance;
while(deposit !=1)
{
cin>>deposit;
if(deposit==1)
break;
endingBalance +=deposit;
}
cout<<"Enter withdrawal amounts";
double withdrawal = 0.0;
while(withdrawal != -1)
{
cin>>withdrawal;
if(withdrawal==-1)break;
endingBalance-=withdrawal;
}
cout<<"Ending balance in the account is:"<<fixed<<setprecision(2)<<endingBalance<<endl;}"
Does anyone know where I went wrong? And if you could reply with the corrected code it would help tremendously. I am very, very green and new to programming in general so I wouldn't be surprised if its riddled with errors.
Any criticism (constructive) is appreciated!
1
u/eatmorepies23 Sep 26 '22 edited Sep 27 '22
Does your autograder list what inputs cause an infinite loop? Knowing this would allow us to retrace its steps to figure out how this could occur.
Edit: If you cannot access the autograder, I would look into the possibility that it might be trying to enter in letters instead of numbers. When I tried to read std::cin with a string to assign to a double variable, the program hangs. Have you learned about input validation yet?
1
u/KuntaStillSingle Sep 26 '22
Is deposit supposed to break the loop when it is given input of 1 or input of -1?
3
u/altorelievo Sep 26 '22
As /u/eatmorepies23 has already been questioning without having some idea of the input of the "autograder" it's difficult to tell.
I can definitely say having a
breakwith the same condition as thewhilestatement seems superfluous.