Not sure what to call this

This is a discussion on Not sure what to call this within the C++ Programming forums, part of the General Programming Boards category; How would I divide something like this by 2. Here is the code you'll need to see. Code: char message[100]; ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    70

    Not sure what to call this

    How would I divide something like this by 2. Here is the code you'll need to see.

    Code:
    char message[100];
    cin.getline(message, 99);
    for(int x=0; x<=strlen(message)-1; x++)
    {
    //...
    }
    Here is my question... I want to make char message a fake integer...which i can do by doing.. (int)message[x];

    My main question is How would I divide the value of (int)message[x] by 2...not the value of the char but the ASCI value.

    I've tried (int)message[x]/=2; and (int)message[x]=/2;
    and neither of those worked, even though i knew the second one wouldn't work. Yea..how would i do that?

    Edit::I don't know if it makes a difference...but I also want to return the value (int)message[x] to (int)message...although it isn't necessary. But how else would i do it, make the value of an array element in terms of ASCI equal to an integer? <---How?
    Last edited by Extol; 10-12-2002 at 01:45 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    int half = (int)message[x] / 2;

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    70

    ..the code...what's wrong

    Ok, I've been sick these past few days...so I'm really not thinking right, and this is annoying me to much. So, yes...what's wrong with this...It's not giving a correct binary number...

    Code:
    #include <iostream>
    using namespace std;
    main()
    {
    	char message[100];
    	int half;
    	cout<<"Enter a message to convert into Binary" <<endl;
    	cin.getline(message, 99);
    	for(int x=0; x<=strlen(message)-1; x++)
    	{
    		if((int)message[x]>=128) cout<<"1";
    		else cout<<"0";
    		half = (int)message[x] / 2;
    		if(half>=64) cout<<"1";
    		else cout<<"0";
    		half = (int)message[x] / 2;
    		if(half>=32) cout<<"1";
    		else cout<<"0";
    		half = (int)message[x] / 2;
    		if(half>=16) cout<<"1";
    		else cout<<"0";
    		half = (int)message[x] / 2;
    		if(half>=8) cout<<"1";
    		else cout<<"0";
    		half = (int)message[x] / 2;
    		if(half>=4) cout<<"1";
    		else cout<<"0";
    		half = (int)message[x] / 2;
    		if(half>=2) cout<<"1";
    		else cout<<"0";
    		half = (int)message[x] / 2;
    		if(half>=1) cout<<"1 ";
    		else cout<<"0 ";
    	}
    	cout<<endl;
    	return 0;
    }
    Edit::The problem isn't in the (int)message[x] either...caz that does show the correct ASCI #...it's all that binary stuff that's screwing up.
    Edit2::I think the problem is in that half is not storing the values...as in...in the last if statement....half is the same value as the first if 2nd if statement...but how do i fix that...
    Last edited by Extol; 10-12-2002 at 02:03 PM.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    11
    try sliding all of the ascii bits to the right, then typecasting the result as an integer. you will always lose the LSD, but it will only matter if it's a 1
    --Cid666

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    11
    also, try setting up your half variable as an array with the same value as x in your character array


    Code:
    half[x] = (int)message[x]   // etc...
    --Cid666

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. minix system call pls help for project
    By porvas in forum Linux Programming
    Replies: 2
    Last Post: 06-14-2009, 03:40 AM
  2. Error C2664 - Trying to call an external Dll
    By jamez05 in forum C++ Programming
    Replies: 3
    Last Post: 08-08-2006, 07:07 AM
  3. Class won't call
    By Aalmaron in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2006, 05:57 PM
  4. Iterative Tree Traversal using a stack
    By BigDaddyDrew in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2003, 05:44 PM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 06:06 PM

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