about int to char casting, string and char*

Hello there, internet is full of these questions but any of them seems to fit my problem.
It's been a week since I started programming again, being busy with other university stuffs. So I will really appreciate your help.

Anyway, I'll write a pair of lines to illustrate you the quandary:


Let's think I've inizialized those two elements:

1
2
3
  char array[15] = "words and 1"   //smth like "abc 1"; [15] is purely academic
  char* ptr = array;


My aim is to convert that "1" at the end of the array with another number, so that it would print "words and 2" or else. I need to change that final number because I have a function that prints a graph and TCanvas (from ROOT reference) has as input variables a char* (which actually defines the name of the graph), but being this function inside a do-while loop, I can't directly call the function and insert the char* I want.

As example I tried the following lines:

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

int n = 5;

if( char b = static_cast<char>(n) )
     array[10] = n;

//or else

int n = 5;

if( char b = static_cast<char>(n) )
     ptr[10] = n;

//or else

int n = 5;

if( char* b = static_cast<char*>(n) )
     ptr[10] = n;





using usual casting:

1
2
3
4
5
6
7

int n = 5;

char b = (char)n;

array[10] = b;


I've tried also with dynaimc_cast, if you need to know.

Nothing seems to work as it should, printng wierd symbols instead of the declared number.

I'm asking you to make me clear those passages and give me some references to look over, maybe I'm just confuse how to use properly these tools.

Thanks for the support.
Last edited on
All your if statements look wrong.

= is not the same a ==

Also, 2 is the number 2. '2' is a char. They're different things. If you attempt to cast the number 2 to a char, you get the non-printing character known as "STX" - https://www.asciitable.com/. The char '2' has numerical representation 50, as you can see in the table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Example program
#include <iostream>
#include <string>

using namespace std;

int main()
{
   char array[15] = "words and 1";
   array[10] = '2';
   
   cout << array;
	
}

Last edited on
if you are stuck with char *, you can do this (I am assuming you trust your data to have a number in the string, if it is possible to have a badly formed string, you need checks for that). I also assume you have enough room in the string. This will do what you asked in the C way.
If you want to do string c++ class, we can do the same thing with slightly different syntax and function calls but same concept.

You don't need much here. Casting, logic, etc... all unnecessary.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

for(int i = 0; i < maxstrsizethatwas15; i++) //lets find the start of the number and end the string there.
{
     if(isdigit(yourstring[i]))
           {
              yourstring[i] = 0;
             break; 
          }
}

and put the value you want into the string at the end now.
char tmp[10];
sprintf(tmp, "%i", newnumber);
strcat(yourstring,tmp); 


Last edited on
Hi guys, thanks for the replies.

All I had to do is give the numerical rapresentation of char '2' which is, as Repeater said, the symbol 50.

I'll write here the portion of code in interest, so anyone with the same question will see it work:

The function is the following:


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

void StampaDifferenza(char* passo, TGraph* a, TGraph* b) {

		
		TCanvas* c1 = new TCanvas(passo, "Differenze Relative");
		c1->cd();
		a->Draw("AL");
		b->Draw("AL");


		int z = string(passo).length() + 5;

		char* stampa = new char[z];
		strcpy( stampa, passo);
		
	
		c1->Print( strncat( stampa, ".jpg", 4 ) );

}



and the loop in question works as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

//including stuff..

        TGraph *posizioneKutta = new TGraph();
	TGraph *posizioneEulero = new TGraph();

        char nome[15] = "graph h=10-1";
	char* passo = nome;
	int n = 50;             //the graphs will have as names: "graph h=10-1", "graph h=10-2", "graph h=10-3"

        double m_h =exp(-1);


			do {

                                //for loop that fill the graphs..

				
				StampaDifferenza(passo, posizioneKutta, posizioneEulero);

				n++;	

                                passo  = nome;    //is that necessary? does it restore all passo elements to in initial state?

				char b = (char)n;    
				passo[13] = b;
								
				m_h = m_h*exp(-1);

				
		
              

			} while( m_h <= exp(-3) );



If have anything to point me out, I'm all ears(or eyes).
Last edited on
char n = 50; //if n is a char, make it a char...

.. stuff

//char b = (char)n; <- lose this line, its just clutter.
passo[13] = n; //no casting needed now

----------
and I have to ask, shouldnt it be
passo[13] = n++;

instead of
n++ //now n is 51, is that what you wanted?!
passo[13] = n; //n is 51 here, not 50

and finally, my previous post works if you need to make it have text 10, text 11, text 12, ...
but yours only works from 0 to 9, then it will misbehave. Is that a problem for you?
Last edited on
can anyone please help to implement this question , link to the page is -:

i have created a new thread on this link

http://www.cplusplus.com/forum/beginner/238539/
hey jonnin,

but yours only works from 0 to 9, then it will misbehave. Is that a problem for you?


I only need to iterate the process three times, so there is no problem with that.

Anyway, I just want to point out that if I write:

1
2
3

int n = 50;  //instead of inizialize "n" as char
passo[13] = n;


It will work too, without involving any char or cast whatsoever.

Thanks again to you guys.
great! Yes, char is really just a 1 byte int so you can do that with no trouble, but its clearer to the reader if you made N type char. Doing it your way will generate some warnings if you have the warning level cranked up (as you should, usually). There is no significant effect in compile/function, just readability.

jonnin -
I wanted to apologize for being a total d-bag to you. I'm sorry, man. It won't happen again. I think you are an excellent programmer and enjoy reading your responses and code.
- tpb
Topic archived. No new replies allowed.