11 Replies - 988 Views - Last Post: 22 November 2008 - 10:02 AM Rate Topic: -----

#1 Mustangchick25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-November 08

Need help with C++ program homework

Post icon  Posted 21 November 2008 - 05:27 PM

I need help with my C++ code. My program is to be like a menu and convert binary input to decimal and from decimal input to binary. This is my code so far:

#include <iostream>
#include <string>

using namespace std;

void binary(int number) {
if(number != 3)
{
int remainder = number % 2;
binary(number/2);
cout << remainder;
}
else
cout << number;
}

int main(){

int number;
int choice;

cout << "1. Decimal to Binary\n";
cout << "2. Binary to Decimal\n";
cout << "3. Quit Program\n";
cout << "Enter your Choice: ";
cin >> choice;

switch (choice) {

case '1':
cout << "Please enter an integer: ";
cin >> number;
if (number < 0)
cout << "Error enter positive integer: ";
else {
cout << "The number in Binary is: ";
binary(number);
cout << endl;
}

My problem is that when I type in 1 for conversions from decimal to binary the program does not call the function binary(int number) it just says "press any key to continue." Also, if someone could give me some advice on how to improve this program(this is pretty basic but I wanted something to compile) I would appreciate it.

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with C++ program homework

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Need help with C++ program homework

Posted 21 November 2008 - 06:34 PM

Welcome!

Please post your code examples using - :code:
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Need help with C++ program homework

Posted 21 November 2008 - 06:39 PM

View PostMustangchick25, on 21 Nov, 2008 - 04:27 PM, said:

I need help with my C++ code. My program is to be like a menu and convert binary input to decimal and from decimal input to binary. This is my code so far:
#include <iostream>
#include <string>

using namespace std;

void binary(int number) {
     if(number != 3)
     {
               int remainder = number % 2;
               binary(number/2);
               cout << remainder;
               }
               else
               cout << number;
}

int main(){
    
    int number;
    int choice;
    
    cout << "1. Decimal to Binary\n";
    cout << "2. Binary to Decimal\n";
    cout << "3. Quit Program\n";
    cout << "Enter your Choice: ";
    cin >> choice;
    
    switch (choice) {
           
           case '1':
                cout << "Please enter an integer: ";
                cin >> number;
                if (number < 0)
                        cout << "Error enter positive integer: ";
                else {
                     cout << "The number in Binary is: ";
                     binary(number);
                     cout << endl;
                }


My problem is that when I type in 1 for conversions from decimal to binary the program does not call the function binary(int number) it just says "press any key to continue." Also, if someone could give me some advice on how to improve this program(this is pretty basic but I wanted something to compile) I would appreciate it.

Choice is an integer int choice;, but your case statement is expecting a char case '1': Remove the quote marks.
Was This Post Helpful? 0
  • +
  • -

#4 Mustangchick25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-November 08

Re: Need help with C++ program homework

Posted 21 November 2008 - 07:00 PM

Thanks for the help. It works now. So, I wanted ask how would I do binary to decimal conversions? I know how it works like with the binary 11101 goes 1*(2^0), 1*(2^1), 1*(2^2), 0*(2^3), 1*(2^4) = 29. I just don't understand how to put it in code form. B)
Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Need help with C++ program homework

Posted 21 November 2008 - 08:25 PM

Well I'd be tempted to input the binary number into a byte array, keeping track of how many binary digits were input. Then converting the number back to decimal is easily done by indexing the array multiplying each location by a power of 2 and summing the result into an integer variable.

This post has been edited by n8wxs: 21 November 2008 - 08:27 PM

Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is online

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,142
  • Joined: 14-September 07

Re: Need help with C++ program homework

Posted 21 November 2008 - 08:42 PM

There are many, many, many snippets around DIC on base conversion.
Was This Post Helpful? 0
  • +
  • -

#7 Mustangchick25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-November 08

Re: Need help with C++ program homework

Posted 21 November 2008 - 10:22 PM

Ok, so if I put the binary numbers in the array, how do I get the program to multiply each binary number by a power of 2?
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is online

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,142
  • Joined: 14-September 07

Re: Need help with C++ program homework

Posted 21 November 2008 - 10:31 PM

for(int i = 0; i < sizeOfArray; i++)
{
     someVar = arr[i] * 2;
     //do other stuff
}


Was This Post Helpful? 0
  • +
  • -

#9 Mustangchick25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-November 08

Re: Need help with C++ program homework

Posted 22 November 2008 - 12:10 AM

Is thsi code ok:

[code]

#include <iostream>
#include <string>

using namespace std;

void decimal(int number) {
if(number != 1)
{
int remainder = number % 2;
decimal(number/2);
cout << remainder;
}
else
cout << number;
}



int main(){

int number;
int choice;
string binary;
int sizeOfArray;
string arr;

cout << "1. Decimal to Binary\n";
cout << "2. Binary to Decimal\n";
cout << "3. Quit Program\n";
cout << "Enter your Choice: ";
cin >> choice;

switch (choice) {

case 1:
cout << "Please enter an integer: ";
cin >> number;
if (number < 0)
cout << "Error enter positive integer: ";
else {
cout << "The number in Binary is: ";
decimal(number);
}

case 2:
cout << "Enter a Binary value: ";
cin >> binary;
for (int i =0; i < sizeOfArray; i++){
int someVar = arr[i]*2;
cout << "The value in Decimal is: " << someVar << endl;
}
} [code]

I know something is wrong with my second case because when I compiled it was a countinous loop. I also don't know if it will convert binary to decimal properly. So, please help.
Was This Post Helpful? 0
  • +
  • -

#10 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Need help with C++ program homework

Posted 22 November 2008 - 12:40 AM

No, it's not ok. :) :)
case 2:
    cout << "Enter a Binary value: ";
    cin >> binary;

    for (int i =0; i < sizeOfArray; i++){
        int someVar =  arr[i]*2;
        cout << "The value in Decimal is: " << someVar << endl;
    }


You copy the input string to binary. The for() loop checks sizeOfArray which is never initialized, which is why your program runs forever. (if you wait long enough it will exit.)
Use for (int i =0; i < binary.length(); i++){

Next, you copy arr[i] which also is not initialized. I think you want binary[i] here. Finally, you copy the individual numbers to someVar. You need to add them someVar += binary[i]*2. someVar needs to be initialized to zero before starting the loop.

This post has been edited by n8wxs: 22 November 2008 - 12:43 AM

Was This Post Helpful? 0
  • +
  • -

#11 Mustangchick25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-November 08

Re: Need help with C++ program homework

Posted 22 November 2008 - 09:07 AM

Is this what you meant?

  case 2:
			   cout << "Enter a Binary value: ";
			   cin >> binary;
			   for (int i =0; i < binary.length(); i++){
				   someVar +=  binary[i]*2;
				   cout << "The value in Decimal is: " << someVar << endl;





Up at the top I have someVar = 0 and binary is still a string.
Was This Post Helpful? 0
  • +
  • -

#12 Mustangchick25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-November 08

Re: Need help with C++ program homework

Posted 22 November 2008 - 10:02 AM

Also, the code up above prints the result 4 times and I just want to print it once. Can anybody help me?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1