Hi guys,
I am trying to create to parse the content of a char array into integer as follows but I cant do it and keep getting the following error. Any solution or suggestion is welcome.
*******************************************
#include <stdio.h>
#include <ctype.h>
int main()
{
char isbn[20];
int weight[10] = {10,9,8,7,6,5,4,3,2,1};
int numericisbn[20];
int product;
int sum = 0;
printf("Enter your ISBN number: ");
fgets(isbn, 20, stdin);
int i;
for (i = 0; i < strlen(isbn); i++)
{
numericisbn[i] = atoi(isbn[i]);
}
printf("%d\n", numericisbn[0]);
printf("%d\n", numericisbn[1]);
printf("%d\n", numericisbn[3]);
}
*******************************************
The code compiles and the error message I get is:
Segmentation fault(core dumped)
Any help or suggestion is appreciated.
10 2358 JosAH 11,448
Recognized Expert MVP
error: parse error at end of input
Any help or suggestion is appreciated.
That is a compilation error; most likely you're missing a right curly brace near
the end of your code. Check them carefully and you'll find the missing one.
The atoi() function takes a '\0' terminated string and returns the int represented
by that string; it shouldn't be a problem if your array contains just digits.
kind regards,
Jos
That is a compilation error; most likely you're missing a right curly brace near
the end of your code. Check them carefully and you'll find the missing one.
The atoi() function takes a '\0' terminated string and returns the int represented
by that string; it shouldn't be a problem if your array contains just digits.
kind regards,
Jos
Yes, it my code was missing closing curly braces of main() function.
The problem I had was parseing error which after fixing the code is:
string.c: In function `main':
string.c:22: error: parse error before '{' token
string.c: At top level:
string.c:26: error: parse error before '}' token
My array is meant to take numbers and dashes "-" which is why I am checking (or rather trying to) each element of the array and sort it into another array of type int (called numericisbn).
The article is about ISBN of books. As you know it comes with dashes alongside numbers, I am basically trying to filterout the dashes.
Regards.
Alien.
JosAH 11,448
Recognized Expert MVP
Yes, it my code was missing closing curly braces of main() function.
The problem I had was parseing error which after fixing the code is:
string.c: In function `main':
string.c:22: error: parse error before '{' token
string.c: At top level:
string.c:26: error: parse error before '}' token
Told you so ;-) We'd have to see the whole code if you want those parse errors
solved. Seeing the error diagnostic messages it has to do with curly brackets
that aren't balanced; same as the previous errors.
kind regards,
Jos
Told you so ;-) We'd have to see the whole code if you want those parse errors
solved. Seeing the error diagnostic messages it has to do with curly brackets
that aren't balanced; same as the previous errors.
kind regards,
Jos
Thanks. :) The code is just above.
Thanks for going through the trouble.
JosAH 11,448
Recognized Expert MVP
Thanks. :) The code is just above.
Thanks for going through the trouble.
Huh? you edited your original post: see your text and the part I thought I quoted.
Now you're talking about a runtime error: you're invoking the atoi() function on a
single char; that is not correct; atoi() takes a pointer to char(s) as its parameter,
terminated by a '\0' char.
kind regards,
Jos
Huh? you edited your original post: see your text and the part I thought I quoted.
Now you're talking about a runtime error: you're invoking the atoi() function on a
single char; that is not correct; atoi() takes a pointer to char(s) as its parameter,
terminated by a '\0' char.
kind regards,
Jos
Since atoi() wont work, is there any other functions that you recommend? I am fairly new to the language and there isn't many that i know of that's likely to do the jobs for me.
Thanks and regards.
Ok, I got down to here:
************************************************** ***
for (i = 0; i < strlen(isbn); i++)
{
if(isdigit(isbn[i]))
{
numericisbn[i] = isbn[i];
puts("isdigit is working\n");
}
}
printf("These are the contents of numericisbn: ");
printf("%d\n", numericisbn[0]);
printf("%d\n", numericisbn[1]);
printf("%d\n", numericisbn[2]);
printf("%d\n", numericisbn[3]);
printf("%d\n", numericisbn[4]);
printf("%d\n", numericisbn[5]);
The output is:
Enter your ISBN number: 12s2
isdigit is working
isdigit is working
isdigit is working
These are the contents of numericisbn: 49
50
0
50
0
0
0
0
0
************************************************** ***
All is good as expected. When I put the "s" amonst the number, the isdigit() function sorted it out and put a 0 in place.
Now those values I get for 1 2 and 2 are ASCII equivalent. Can someone tell me how to convert those ASCII to proper ints?
The issue obviously lies in the line bolded above. Since atoi() wont work I have exhausted all my knowledge on this. Any ideas anyone?
Regards.
BTW, the reason why you see all the 0s as the output of printf() is because i ran a for loop before the code above which clears the numericisbn[10] of all its memory addresses and puts 0 inplace.
In your loop you can do following changes
for (i = 0; i < strlen(isbn); i++)
{
if(isdigit(isbn[i]))
{
char s[2];
s[1] = '\0';
s[0] = isbn[i];
numericisbn[i] = atoi(s);
printf("%d", numericisbn[i]);
puts("isdigit is working\n");
}
}
Since we have only one api(atoi) which converts integer strings to inytegers, it is better to make it a temp string and get an int.
In your loop you can do following changes
for (i = 0; i < strlen(isbn); i++)
{
if(isdigit(isbn[i]))
{
char s[2];
s[1] = '\0';
s[0] = isbn[i];
numericisbn[i] = atoi(s);
printf("%d", numericisbn[i]);
puts("isdigit is working\n");
}
}
Since we have only one api(atoi) which converts integer strings to inytegers, it is better to make it a temp string and get an int.
Thanks a lot mate. I'll try the solution above when i get a chance.
Regards.
Yep it work.
Once again thank you very much.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Nut Shell |
last post by:
I needed to parse a csv file for my project. Could not find any so I
put together this code. It works for general cases. Use it and let me
know. Just
Cut and paste the 2 functions in a new Form...
|
by: marco |
last post by:
I'm trying to parse a xml bookmarkpage with php. I found a very useful
example script about how you can parse a xml document with php. The
scriptworks really smooth. The xml test document (See...
|
by: Stargate4004 |
last post by:
Hi,
I have am converting a VB6 application to VB.net and have run into a
problem. The application connects to a TCP port on a Linux box and
receives a data buffer. The data buffer contains...
|
by: irishdudeinusa |
last post by:
Hello Everyone,
I have been working a webservice where I can use it in other
applications that I am working on. However, I am running into a problem
with the data returned and I was wondering if...
|
by: Klaus Alexander Seistrup |
last post by:
Hi group,
I am new to xgawk (and seemingly to xml also), and I've been struggling
all afternoon to have xgawką parsing an XHTML file containing a hCard˛,
without luck. I wonder if you guys...
| |
by: Cuong.Tong |
last post by:
Greeting,
I am writing my own web server and having some problme parsing the the
mulitpart/form-data
stream that is sent from the browsers.
I have a form looks something like this
<form...
|
by: christianlott1 |
last post by:
What this function attempts to do is remove the first string of
numbers in an address field.
I get "invalid procedure call or argument" for line:
----
j = Asc(Mid(Original, i, 1))
----
...
|
by: cjl |
last post by:
As a learning exercise, I am trying to write a web-based version of
'drawbot' in PHP.
See: http://just.letterror.com/ltrwiki/DrawBot
I am interested in hearing ideas about how to approach...
|
by: i_robot73 |
last post by:
I have a file, containing hex values for dates (MMDDYYYY)<status
code><??such as:
...
|
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...
|
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,...
| |
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |