Oct 28, 2014 at 8:51pm UTC
allow the user to run the program more than once if so desired.
prompt the user to try again Y/N at the end, This program works fine below.but doesn't loop how would i go about it
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <math.h>
#include <iomanip>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int a,b, sum = 0; // Variables
//Asking the user for input
cout <<"\n\n\t Please Enter A Number For a "; // Number for a
cin >> a; // input
cout <<"\n\n\t Please Enter A Number For b "; // Number for b
cin >> b; //input
while (b!=1)
{
if(b%2>=1)
{
sum+=a;
}
b=b/2;
a=a*2;
}
sum+=a;
cout << "\n\n\t The answer is " << sum << "\n\n";
_getch();
return 0;
}
Oct 28, 2014 at 9:27pm UTC
Not hard - initialize a char continue = 'y', then start a block with while ( continue == 'y' || continue == 'Y'), and before you end the block ask the user to enter y or n to calculate with another set of values.
Oct 28, 2014 at 10:00pm UTC
Hi,
1) You don't need all libraries....you just need
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
2) why you need to do b = b/2; if after that you don't used....
3) If you want to add each number, you have to put into a loop....
Check again
Oct 28, 2014 at 10:04pm UTC
Thanks for the help guys am just started programing and its not coming easy to me all advice is much appreciated