I need use 1 if/else for repetitive use and can use more for input constraints. if you enter 345 and 2 bits obviously it wouldn't work (1 if) and in the while the pow(2,b)<=0. I know b=b-1 if it had to be setup like this.
it's for an assignment. All we can use are whiles, if/else. If you enter a number (say 332) and 2 bits the program shouldnt work. that is one if. then repitive in while (b==0) is the other if i believe but how do i setit up ?
char a='1';
int n, b ;
while(a=='1')
{
system("color f0");
cout<<"Enter a number, and an amount of bits. If it is possible this will\n determine the decimal to binary value\nEnter the number: ";
cin>>n;
cout<<endl<<"Enter the number of bits: ";
cin>>b;
cout<<endl<<endl;
b=b-1;
if
//while(b==0)
cout<<"Enter 1 to continue or anyother number to discontinue: ";
cin>>a;
cout<<endl;
}
As I understand it, this comment set describes what you want to accomplish:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int main()
{
// initialize necessary variables for a (user continue), number, bits, binary
// while the user entry(a) = 0
{
//accept input for number
//accept input for bits
//if the number is greater than allowable for bits (pow(2,bits) < number)
// reject the entry
//else
// calculate the binary value of number
// print the binary value
//end if
//ask the user if he/she wants to continue, input b
}
return 0
}
Now, for each line in the text version, write a line or more as necessary to fulfill the statement:
// add all the #includes that need to go here
int main()
{
// initialize necessary variables for: int b (user continue), number, bits, binary
int a = 1;
//you only need to initialize them when you write the code that uses them.
// while the user entry(a) = 0
while (a == 1)
{
// accept input for number:
std::cout << "Enter a number: ";
std::cin >> number;
//accept input for bits
std::cout << "Enter bits:";
std::cin>> bits;
//if the number is greater than allowable for bits (pow(2,bits) < number)
if(pow(2,bits) < number)
{
//reject the entry
}
//else
else
{
//calculate the binary value of number
//print the binary value
}
//ask the user if he/she wants to continue, input b
std::cout << "Enter 1 to continue, any other number to discontinue:"
std::cin >> a;
} // end of while (a == 1) loop
std::cout << "Goodbye!\n";
return 0;
}
As you add a block of code, try compiling and running it, even if you don't have anything to print out yet. Then add another block, try it. . .etc. That way if you do have a small error somewhere you 'll have a lot better chance to find it quickly.
I'm sure it is tempting to document your code (usually a class requirement) after you complete the assignment, but I assure you, if you use your documentation to create and complete a plan it can save a lot of pain and suffering.