-2

I am trying to make a back deposit system that requires user to deposit amount of 65000, account holder should be keep deposing money until full amount of 65000 is paid. I tried to do this with do while loop but it is just working as variable and not checking how much money is paid. Here is sample of code:

int user_fee;
int deposited=65000;
do
{
    Console.WriteLine("please enter fee");
    user_fee = Convert.ToInt32(Console.ReadLine());
    obj.Fee = user_fee;
} while (user_fee<= deposited);
jmattheis
  • 10,494
  • 11
  • 46
  • 58
Omar Aeo
  • 21
  • 3
  • 1
    Possible duplicate of [do while loop in C#](https://stackoverflow.com/questions/2539009/do-while-loop-in-c-sharp) –  Nov 24 '17 at 17:24
  • @XavierBalloy The issue is far more trivial than the suggested duplicate. :-) – Sipty Nov 24 '17 at 17:53

2 Answers2

0

Currently you're replacing user_fee with the input fee infinitely.

Change

user_fee = Convert.ToInt32(Console.ReadLine());

to

user_fee += Convert.ToInt32(Console.ReadLine());

This way, you'll keep adding the new fee into user_fee.

EDIT:

Full solution, as it seems I wasn't clear:

int user_fee;
int deposited=65000;
do
{
    Console.WriteLine("please enter fee");
    user_fee += Convert.ToInt32(Console.ReadLine());
    obj.Fee = user_fee;
} while (user_fee<= deposited);

Let me know if this works for you. Also, why's the reason for using obj.Fee = user_fee?

Sipty
  • 1,159
  • 1
  • 10
  • 18
  • sorry for my idiotic question...bro i am beginner programmer and thanks for your reply. i tried to do this but it throwing error do { Console.WriteLine("please enter fee"); user_fee = Convert.ToInt32(Console.ReadLine()); obj.Fee = user_fee; user_fee +=; // error (invalid expression) } while (user_fee<= deposited); – Omar Aeo Nov 24 '17 at 17:31
  • @OmarAeo That's because you need to use `int user_fee = 0;` – Camilo Terevinto Nov 24 '17 at 17:33
  • I see where the error is Omar. `user_fee +=;` is not a valid expression. :-) Give me a minute and I'll post the entire loop for you. :-) – Sipty Nov 24 '17 at 17:47
  • o { Console.WriteLine("please enter fee"); user_fee = Convert.ToInt32(Console.ReadLine()); obj.Fee = user_fee; user_fee = user_fee+user_fee; } while (user_fee< deposited); – Omar Aeo Nov 24 '17 at 17:49
  • still not wotking – Omar Aeo Nov 24 '17 at 17:49
  • Please update your latest, non-working, version of the loop in the answer with formatting. Also include the error log. :-) – Sipty Nov 24 '17 at 17:51
  • i did please refresh this page – Omar Aeo Nov 24 '17 at 17:55
  • Sipty you have done some magic :) its working now.. but may i know why `user_fee = user_fee+user_fee;` is not same as `user_fee += Convert.ToInt32(Console.ReadLine());` Isnt both statment same? – Omar Aeo Nov 24 '17 at 18:05
  • i am using `obj.Fee` because i am using class object – Omar Aeo Nov 24 '17 at 18:06
  • Hello Omar, user_fee + user_fee is equivalent to user_fee*2 :-) The += adds the new value to the existing one, so in our case it's equivalent to user_fee = user_fee + Convert.ToInt32(Console.ReadLine()); Hope this helps and goodluck! – Sipty Nov 27 '17 at 15:32
0

Sipty you have done some magic :) its working now.. but may i know why user_fee = user_fee+user_fee; is not same as user_fee += Convert.ToInt32(Console.ReadLine()); Isnt both statment same? – user_fee = user_fee+user_fee; is also adding user_fee into itself after each loop iteration btw thanx.. btw i am using obj.Fee because i am using class object

Omar Aeo
  • 21
  • 3