473,386 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

String -> int

4
Hi, I'm having a problem for casting a string to int, so tht evry number in the string becomes a diff int. Cus I get wrong numbers back.

I'll post an example
its sumtin like this:
int main(void){

int x_rij, x_kolom, y_rij, y_kolom;
char spel_request[5];

scanf("%s",&spel_request);
if((int)spel_request != '0'){
x_rij= ((int)spel_request[0]);
x_kolom= ((int)spel_request[1]);
y_rij= ((int) spel_request[3]);
y_kolom=((int)spel_request[4]);
}
printf("\n %d\n %d\n %d\n %d",x_rij,x_kolom,y_rij,y_kolom);

return 0;
}

I'm not so good at this rly, just learning it.

Regards A.
Dec 28 '07 #1
8 1783
weaknessforcats
9,208 Expert Mod 8TB
You can't cast a string to an int.

A string is an array of char. Therefore, the name of the string is the address of element 0. When you cast, you are casting the address of the string to an int.

You will need to call a function to convert for you. Try atoi().
Dec 28 '07 #2
oler1s
671 Expert 512MB
Hi, I'm having a problem for casting a string to int
Casting in C is simple. I think you have it right. (type) variable is the syntax. But I highly doubt you want to cast. I think what you want is to convert the string into a number.

I'm trying to make a point here. I don't know how you got the idea into your head, but casting is not the same as converting. Casting overrides the compiler. You effectively say, this is a variable of one type, but I know so much better than you, and I'm telling you to pretend it's actually another type. Maybe your computer will explode because of that. Whatever. You get the gun, and you get to point it at your foot and pull the trigger. Don't complain.

There is a function in C for converting C strings to numbers. There's two actually. atoi and strtol. Don't use atoi. Use strtol. Google on how to do this.
Dec 28 '07 #3
Aske
4
kk, thx for response. Now I kno at least I'm not going the right way. Will look into the strtol() then.

ty
Dec 29 '07 #4
Aske
4
I don't know how you got the idea into your head, ...
well... the user has to input 4 ints vor ex: "12 34" and these numbers gotta have diffrent names, ex: x=1, y=2, etc... . I thought this was the way to do it, but silly me.
Dec 29 '07 #5
oler1s
671 Expert 512MB
Well, your idea could be made to work. But there are certain implications. Remember that in the end, everything is just 1s and 0s to the computer. Chars are 1s and 0s, and Cstrings are arrays of chars. So you could technically cast a char as an int.

Let me ask you a question. Convert 'a' to a number. Now convert 'b' to a number. You might say a=1 and b=2. Logical, right? What about a=20 and b=21? Well, that works too, right? How about a=31, b=32? Maybe a=33,b=35,c=37? This mapping of numbers to an alphabet is a Character encoding. ASCII is the most well known, but look at systems like EBCDIC and what not. It has g,h, and i in a row. But there's a gap between i and j, and so on. Technically, you could just pretend that ASCII is the only system in existence for your purposes, but you might as well learn to write robust code now, and then fool around later at your peril.
Dec 29 '07 #6
Aske
4
Got it kinda solved this way,
#include ...

int main(void){
char spel_request[5], *hulp;
int a[5], t;

clrscr();
scanf("%5[^\n]*s",&spel_request);
if(spel_request[0] == '0'){
printf("sluiten");
}else{
for (t=0;t<5;t++){
hulp = spel_request[t];
a[t] = atoi(&hulp);
printf("%c ", hulp);
}
printf("\n %d\n %d\n %d\n %d",a[0],a[1],a[3],a[4]);
}

return 0;
}

but the problem now is, when I input it into the program it gives an unending loop thx to scanf("%5[^\n]*s",&spel_request);

any thoughts?

Regards
Jan 2 '08 #7
akuuu
2
Here is a funtion that might do the job.
Returns the converted string

int a_to_i(char *s){
int i = 0, sign;
if(*s == '-'){
sign = -1;
s++;
}
else sign = 1;
while(isdigit(*s)){
i += *s -'0';
i*= 10;
s++;
}
return znak*i/10;
}

btw isdigit is standard function in C that checks if some element of the string is digit or '0' <= x[i] <= '9', ofc x[MAX] is string
Jan 3 '08 #8
akuuu
2
and i forgot u need another function that will turn the integer ex. 1234 to 4321. This looks like this

int reverse(int a)
{
int i;
while(a){
i = a%10;
a /= 10;
i *= 10;
}
return (i/10);
}


thats it.
Jan 3 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
2
by: Badass Scotsman | last post by:
Hello, Using VB and ASP,NET I would like to be able to search a STRING for a smaller STRING within, based on the characters which appear before and after. For example: String1 = " That was...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
11
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove...
8
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.