Having some newb troubles

This is a discussion on Having some newb troubles within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to write a console app that will take in a base 10 number and output that number ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    26

    Having some newb troubles

    Hi,
    I'm trying to write a console app that will take in a base 10 number and output that number in binary form. I created a character array for the binary number and a couple of for loops (one to mod the number and divide. One to print the contents of the array.) but, I must have the syntax of something incorrect. I have looked at the code for a couple hours though and cannot figure out the issue. It seems to me when I run the app like the for loops are not even running. Anyways tia for any help.

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    int main(void) {
      int num = 0;
      int i = 0;
      //array to store the binary digits.  all values are initialized to 0.
      int binary[8] = {0};
      
      cout << "Please enter a base10 number between 0 and 255:" << endl;
      cin >> num;
      
      //loop from 7 to 0 modding the number by 2 and then dividing it in half.
      for (i = 7; i < 0; i--) {
        binary[i] = (num % 2);
        num = num / 2;
      }
    
      cout << "Your number is:" << endl;
      
      //loop through the binary array printing each digit.
      for (i = 0; i > 7; i++) {
        cout << binary[i];
      }
      
      cout << endl;
      	
      return 0;
    }
    Regards,
    ~Joshua Norton

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    12
    Hi in your first for loop you have this

    for (int i = 7; i < 0; i--)

    and it should be this
    for (int i = 7; i > -1; i--)

    in the second for loop you have this

    for (int i = 0; i > 7; i++)

    and it should be this
    for (int i = 0; i < 8; i++)

    Regards William
    Last edited by bigwullie; 11-15-2003 at 08:51 PM.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    26

    ...

    Thanks for the fast response. I knew I was staring too hard and overlooking something silly . Thanks again.
    Regards,
    ~Joshua Norton

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    26

    ...

    err why would the second loop be
    Code:
    for (int i = 0; i < 8; i++)
    when the valid array elements are 0 - 7?
    Regards,
    ~Joshua Norton

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    26

    ...

    Nm. Another retarded question lol. For some reason I had confused the middle argument of the for loop as the "while this condition is false" when in reality it's saying "while this condition is true". I think anyways. Thanks again for the help.
    Last edited by Josh Norton; 11-15-2003 at 09:04 PM.
    Regards,
    ~Joshua Norton

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    it depends what kind of stuff you have in your array .....
    take an string array for eksample......

    MAX specifies the max. length of the string (6 characters for "Monday" plus the terminating null ('\0') makes 7

    You see if you are displaying 7 digits, the loop should be from 0 to 7.....

    take a look at this code
    Code:
    string_array.cpp
    int main()
    {
    const int MAX = 7;

    char MyArray[MAX] = {"Monday"};

    //runing this loop for 7 times

    for( int i=0; i<MAX; i++ )
    cout << MyArray[i] << endl;

    return 0;
    }

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    26

    ...

    Yeah I see what your saying. I had both uses in my app and had both set backwards heh.
    Regards,
    ~Joshua Norton

  8. #8
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by agony


    You see if you are displaying 7 digits, the loop should be from 0 to 7.....
    This sounds confusing; you want to do the loop 7 times, with values from 0-6. Your code itself is correct, because (i = 0; i < 7; i++) runs the loop with i = 0,1,2,3,4,5,6 (as it should).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  9. #9
    Registered User
    Join Date
    Nov 2003
    Posts
    26

    ...

    Well it was almost correct.

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    int main(void) {
      int num = 0, num1 = 0;
      int i = 0;
      //array to store the binary digits.  all values are initialized to 0.
      int binary[8] = {0};
      cout << "Please enter a base10 number between 0 and 255:" << endl;
      cin >> num;
      
      num1 = num;
      
      //loop from 7 to 0 modding the number by 2 and then dividing it in half.
      for (i = 7; i >= 0; i--) {
          binary[i] = (num % 2);
          num = num / 2;
      }
    
      cout << num1 << " in binary is: ";
      
      //loop through the binary array printing each digit.
      for (i = 0; i <= 7; i++) {
        cout << binary[i];
      }
      
      cout << endl;
      
      return 0;
    }
    That is the correct code. Both loops had the condition of the for loop backwards. Also they had to be changed to >= and <= so the last element of the array was included. Why I could'nt figure that out I dunno, seemed like the harder I tried the further I was from getting anywhere heh. I knew the loops were'nt running but I just did'nt put 2 and 2 together. Instead I tried the "stare really hard till you find something" method.
    Regards,
    ~Joshua Norton

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newb question
    By C_ntua in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2008, 10:44 AM
  2. Newb Question Character Counting
    By Wilder in forum C Programming
    Replies: 13
    Last Post: 06-23-2008, 12:37 AM
  3. Replies: 4
    Last Post: 01-15-2006, 05:14 AM
  4. Newb C++ Programmer
    By Philandrew in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2004, 09:44 PM
  5. Detective 1: Who do you think killed him? Detective 2: google it newb.
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 10-09-2004, 04:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21