#include <iostream.h> 
int main() 
{ 
    char word[32]; 
    int x = 0; 
    cout << "Please enter the word (maximum 32 characters):\n"; 
    cin >> word; 
    cout << "The ASCII for this word is:\n"; 
    while (word[x] != '\0')    // While the string isn't at the end... 
    { 
        cout << int(word[x]);    // Transform the char to int 
        x++; 
    } 
    cout << "\n"; 
    return 0; 
}

This is a program convert a string to ASCII.
Example: string is "hi", the output is 104105.
But the progam displays the number 104 then displays the number 105.
So, how can I save the number "104105" in an integer variable to use later?

>This is a program convert a string to ASCII.
No, it's a program that prints the integer value of each character in a string instead of the character representation.

>#include <iostream.h>
You're about ten years too late with this. The correct C++ header is <iostream>, and you need to qualify for the std namespace as well since every name in the standard library is now contained in it.

>cin >> word;
No, this is very bad. If you must use operator >> to read strings, include <iomanip> and force a maximum field width with setw.

>while (word[x] != '\0')
A for loop would be better here, or if you still want to use a while loop, declare x closer to it. C++ lets you declare variables anywhere in your function. They don't have to be at the top of a block.

>cout << int(word[x]);
You probably want some whitespace in the output so that you can tell where one character ends and another starts.

>cout << "\n";
If you're only printing one character, there's no need to use the string "\n". You can use just a single character as well:

cout<<'\n';

>return 0;
This isn't required anymore. 0 is returned automagically when you fall off the end of main (but that rule only applies to main).

This is better:

#include <iostream>
#include <iomanip>

int main()
{
  char word[32];

  std::cout<<"Please enter a word (maximum 32 characters): ";
  std::cin>> std::setw ( sizeof word ) >> word;
  std::cout<<"The integer values for this word are:\n";

  for ( int x = 0; word[x] != '\0'; x++ ) {
    std::cout<< word[x] <<" = "<< int ( word[x] ) <<'\n';
  }
}

>So, how can I save the number "104105" in an integer variable to use later?
How do you plan to use it?

what i think you can do is save each value of int(word[x]) in an integer array...
and then add them like 104*1000+105=sum
and then if it is ghi
103*pow(10, 6)+sum=sum...and so on...
thats just my idea...
there can be a better way...!!

>thats just my idea...
You're making assumptions about what the OP wants to do. Personally I think he's just not communicating well, because concatenating values like that will quickly reach the limit of the integer data types. Since his "word" has an upper limit of 32 characters, overflow or wraparound is guaranteed for all but the shortest of words.

Hi, this is exactly what i'm trying to do. Any ideas would be very helpfull. In effect I want to store the ascii generated numbers in individual locations.

Hi, this is exactly what i'm trying to do. Any ideas would be very helpfull. In effect I want to store the ascii generated numbers in individual locations.

Dear Baalzamon,

Please refer to the last few pages of the hot thread on computing Sqrt without using Sqrt() and Pwr functions. The codes in my program may help you find an answer. Just store the ASCII value after each character was entered, in an array.

c = getch();

c is the ASCII code of the character entered.

regards,

The actual title of the thread is

"Square root program without sqrt or pwr.

Good luck.

regards,

int num=0;
while (word[x] != '\0') // While the string isn't at the end...
{
cout << int(word[x]); // Transform the char to int
num+=int(word[x])
x++;
}

Perhaps the following codes may be useful to gain an idea of how to convert character to ASCII and back by storing ASCII in an int array, and use the array to get the character output back again. This is a reversible process.

The above is demonstrated by assigning values to an C++ string, and keyboard entry using cin.getline() function and C-style string.

Regards,

// ascii2arr.cpp    character to ascii and ascii to char   YBM 29 Sep 2009
#include <iostream>
#include <string>
#include <conio.h>
using std::cin;
using std::cout;
using std::string;
string display(string);
const int size=32;
char cword[size];
string word;
int arr[size]={0};
int x = 0, len=0;
int main()  {
    cout << "\nTHIS PROGRAM DEMONSTRATES CONVERSION FROM CHARACTER TO \nASCII AND BACK USING AN INT ARRAY BY ASSIGNING VALUES \nAND KEYBOARD ENTRY\n";
    cout << "\n\nBy Assigning value to a string...";
    word="Hi there.... What's up?";
    display(word);
    cout << "\nBy keyboard Input...";
    cout << "\n\nEnter sentence \n";
	cin.getline(cword,size);
	display(cword);
    return 0;
}
string display(string word) {
   cout << "\nSentence entered is \n" << word;
   len=word.length();
   cout << "\n\nLength of sentence is " << len;
   cout << "\n\nThe ASCII for each character are as follows :\n\n";
   for (x=0;x < len;x++) {    // While the string isn't at the end...
        cout << word[x] << " : "<< int(word[x]) << "\n";    // Display char and ascii
        arr[x]=int(word[x]);   // storing ascii code of each character in an array
    }
    cout << "\n";
    // reversing ascii to characters
    cout << "Reversing ASCII codes back to characters\n\n ";
    for (x=0;x <len;x++) {
        cout << char(arr[x]);
    }
    cout << "\n\n";
return word;
}

Look, people, its not hard.

Your code:::

#include <iostream.h>
      int main()
      {
      char word[32];
      int x = 0;
      cout << "Please enter the word (maximum 32 characters):\n";
      cin >> word;
      cout << "The ASCII for this word is:\n";
      while (word[x] != '\0') // While the string isn't at the end...
      {
      cout << int(word[x]); // Transform the char to int
      x++;
      }
      cout << "\n";
      return 0;
      }

Your new code:::

#include <iostream.h>
      int main()
      {
      char word[32];
      int x = 0;
      [B]int ascii[32];[/B]
      cout << "Please enter the word (maximum 32 characters):\n";
      cin >> word;
      cout << "The ASCII for this word is:\n";
      while (word[x] != '\0') // While the string isn't at the end...
      {
      [B]ascii[x] = int(word[x]); // Transform the char to int[/B]
      x++;
      }
     [B] //Now, to display the array with the asciis...
     for (x=0; x<32; x++) {
     cout << ascii[x] << ' ';
     }[/B]
      cout << "\n";
      return 0;
      }
commented: Don't resurrect dead threads. And step up to moderns standards for a compiler, too, while you're at it. -2

hello,
I want to write the same codes.but I want to store the ascii numbers in two dimensional array instead of one dimensional array.could you help me please?!

hello,
I want to write the same codes.but I want to store the ascii numbers in two dimensional array instead of one dimensional array.could you help me please?!

here are my codes:-
#include <iostream>
#include<fstream>
#include <string>
using namespace std;
string display(string);
main()
{
    string word;
    ifstream infile;
    infile.open ("text.txt");
        while(!infile.eof()) 
        {
            getline(infile,word); 
            cout<<word<<endl; 

            }
            display(word);
    infile.close();

    system ("pause");
}
string display(string word) {
    int c[2][2];
   int x = 0, len=0;
   len=word.length();
   cout << "\n\nLength of sentence is " << len;
   cout << "\n\nThe ASCII for each character are as follows :\n\n";
   for (x=0;x < len;x++) { 
   for(int i;i<len;i++){
    c[x][i]=0;
    for(int k=0;k<2;k++){
        c[x][i]+=word[x][k];

    }
   }   


    }
    for(int x=0;x<2;x++){
    for(int i=0;i<2;i++){
        cout<<c[x][i]<<" ";
    }
cout<<endl;   

}

    //cout << "Reversing ASCII codes back to characters\n\n ";
    //for (x=0;x <len;x++) {
        //cout << char(word[x]);
    //}
    //cout << "\n\n";
return word;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.