Reputation: 288
I am a very beginner in c++. i am just learning abc of this language.. i created this small program that would add:
#include <iostream>
using namespace std;
float add(float a, float b){
return a+b;
}
int main(){
float num1;
float num2;
cout<<"add...enter digits \n";
cout<<"first digit: ";
cin>>num1;
cout<<"\n Second number: ";
cin>>num2;
cout<< "your sum is: "<<add(num1, num2)<<endl;
system("pause");
}
this above code is my very first usable application of c++
now i wanted that when some one wants to again add then this program starts again... i thoughts of using loops, i but cannot think how to use in such a way. i mean what conditions i should use.
please tell me
thanks.
Upvotes: 0
Views: 26655
Reputation: 71
Just call main(); again.. in your code will be like this:
#include <iostream>
using namespace std;
float add(float a, float b){
return a+b;
}
int main(){
float num1;
float num2;
cout<<"add...enter digits \n";
cout<<"first digit: ";
cin>>num1;
cout<<"\n Second number: ";
cin>>num2;
cout<< "your sum is: "<<add(num1, num2)<<endl;
main();
}
Upvotes: 0
Reputation: 877
Here is how we do :
#include <iostream>
using namespace std;
float add(float a, float b){
return a+b;
}
int main(){
float num1;
float num2;
while(true)
{
cout<<"add...enter digits \n";
cout<<"first digit: ";
cin>>num1;
cout<<"\nSecond number: ";
cin>>num2;
cout<< "your sum is: "<<add(num1, num2)<<endl;
char ch = 'n';
cout << "Start Again, [y/n] ? ";
cin >> ch;
if (ch == 'Y' || ch == 'y')
continue;
else
break;
}
return 0;
}
Upvotes: 3
Reputation: 206717
Since you are starting, I am going to suggest changing your code a little bit:
#include <iostream>
using namespace std;
float add(float a, float b)
{
return a+b;
}
// Function that does the core work.
void read_input_print_sum()
{
float num1;
float num2;
cout<<"add...enter digits \n";
cout<<"first digit: ";
cin>>num1;
cout<<"\n Second number: ";
cin>>num2;
cout<< "your sum is: "<<add(num1, num2)<<endl;
}
int main()
{
read_input_print_sum();
system("pause");
}
Now, you can add various methods to call the core function repeatedly. One has been suggested in the answer by Rakibul Hassan.
That can be implemented with:
int main()
{
while (true)
{
read_input_print_sum();
}
system("pause");
}
Another way: Ask the use whether they want do the work again.
bool getRepeat()
{
cout << "Do you want to repeat? (Y/N): ";
int yesno = cin.getc();
return ( yesno == 'Y' || yesno == 'y' );
}
int main()
{
bool repeat = true;
while (repeat)
{
read_input_print_sum();
repeat = getRepeat();
}
system("pause");
}
Another way: Ask the number of times they wish to repeat the computation before you start.
int main()
{
int N = 0;
cout << "How may times do you want to add numbers: ";
cin >> N;
for ( int i = 0; i <= N; ++i )
{
read_input_print_sum();
}
system("pause");
}
Upvotes: 2
Reputation: 21
You can try putting the main part of your 'adding' in an endless loop. I suggest use a post condition loop, meaning one that will execute it's body at least once (then it will check the condition and so on), because you'll be wanting to add some numbers at least once.
Example:
do {
// do stuff here
} while (true) // always true condition -> makes the loop infinite
So I guess you'll ask how do you stop this. You can ask the user if he wants to continue. Add this to the loop's body:
int lock = 0;
cout << "Do you want to continue? (0 = no, 1 = yes)" << endl;
cin << lock;
if (lock == 0) break; // stops the loop immeadiately
You can do the same with lock being char with values 'y' or 'n'.
Upvotes: 2
Reputation: 8697
If we were to take "start from the beginning" literally, we can call main()
again when we get to the end!
#include <iostream>
using namespace std;
float add(float a, float b){
return a+b;
}
int main(){
float num1;
float num2;
cout<<"add...enter digits \n";
cout<<"first digit: ";
cin>>num1;
cout<<"\n Second number: ";
cin>>num2;
cout<< "your sum is: "<<add(num1, num2)<<endl;
system("pause");
return main(); // <---- starts again from beginning of main()!!
}
(This will eventually crash when the program runs out of stack space, but the user will almost certainly get tired of adding numbers long before then. Of course, a clever compiler would realize this is tail recursion, and use a goto
instead of a function call.)
Upvotes: 2
Reputation: 7625
The following code will do that:
#include <iostream>
using namespace std;
float add(float a, float b){
return a+b;
}
int main(){
float num1;
float num2;
while( true ){
cout<<"add...enter digits \n";
cout<<"first digit: ";
cin>>num1;
cout<<"\n Second number: ";
cin>>num2;
cout<< "your sum is: "<<add(num1, num2)<<endl;
}
system("pause");
}
above code will run forever. If you want to give user a choice, then apply a loop break condition.
char repeat = 'y';
while( repeat == 'y'){
// do as previous
//.....
//finally give user a choice
cout<< "Do you want to repeat?(y/n):";
cin>> repeat;
}
system("pause");
}
Upvotes: 1