473,327 Members | 2,081 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,327 software developers and data experts.

atoi conversion problem in c

I have the following program:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. void main ()
  5. {
  6.   char command[20];
  7.   char *com,*pos,*stack1,*stack2;
  8.   int boxes,boxtable[25][26],i,j,k,st1,st2;
  9.   int check=1;
  10.   for(i=0;i<25;i++){
  11.       for(j=0;j<26;j++){
  12.           if(j==0||j==1)
  13.               boxtable[i][j]=i;
  14.           else
  15.               boxtable[i][j]=0;
  16.       }
  17.   }
  18.   scanf("%d",&boxes);
  19.   gets(command);
  20.   while(strcmp(command,"quit")!=0){
  21.     com=strtok(command, " ");
  22.     stack1=strtok(NULL, " ");
  23.     pos=strtok(NULL, " ");
  24.     stack2=strtok(NULL, " ");
  25.     st1=atoi(stack1+0);
  26.     st2=atoi(stack2+0);
  27.     if(st1==st2)
  28.     check=0;
  29.     for(i=0;i<boxes;i++){
  30.         for(j=1;j<=boxes;j++){
  31.             if(boxtable[i][j]==st1||boxtable[i][j]==st2){
  32.                 for(k=j;k<=boxes;k++){
  33.                     if(boxtable[i][k]==st1||boxtable[i][k]==st2)
  34.                         check=0;
  35.                 }
  36.             }
  37.         }
  38.     }
  39.     if(check==1)
  40.         printf("it's true!");
  41.     gets(command);
  42.   }
  43.   for(i=0;i<boxes;i++){
  44.       for(j=0;j<boxes;j++){
  45.           printf("%d",boxtable[i][j]);
  46.           if(j==0)
  47.               printf(":");
  48.           printf(" ");
  49.       }
  50.       printf("\n");
  51.   }
  52. }
The problem is that using the atoi function makes my program crash after I input the first line (boxes). When I delete the two uses of atoi everything works fine! The problem is I need to convert stack1 and stack2 into integers somehow...any ideas?
Feb 5 '07 #1
2 2207
horace1
1,510 Expert 1GB
a problem is
Expand|Select|Wrap|Line Numbers
  1.   scanf("%d",&boxes);
  2.   gets(command);
  3.  
when you type the number followed by newline scanf() returns the number in boxes and the newline is left in the input stream. when gets() is called it returns immediatly with an empty string. try
Expand|Select|Wrap|Line Numbers
  1.   scanf("%d",&boxes);
  2.   getchar();    // remove newline from input stream
  3.   gets(command);
  4.  
Feb 5 '07 #2
Thanks a lot that solved the problem!!
Feb 6 '07 #3

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

Similar topics

19
by: Mike Moum | last post by:
I think there may be a bug in string.atoi and string.atol. Here's some output from idle. > Python 2.3.4 (#2, Jan 5 2005, 08:24:51) > on linux2 > Type "copyright", "credits" or "license()"...
4
by: senthil kumar | last post by:
Hi We've been noticing a problem with atoi conversion on PowerPC. When we supply value > 2^31-1 to utmost 2^32-1 it doesn't work properly. What i mean is the converted value when printed using...
15
by: Anonymousgoogledeja | last post by:
Hi all, since the function atof, atoi, _atoi64, atol returned value are Return Values Each function returns the double, int, __int64 or long value produced by interpreting the input...
47
by: sudharsan | last post by:
Hi could you please explain wat atoi( ) function is for and an example how to use it?
5
by: David Ching | last post by:
Hello, I'm a .NET newbie but have 12 years of C++/MFC. In C++, atoi() stops converting at the first non-digit, so in this code, octet will be 192: int octet = atoi("192."); // <-- note...
4
by: sachinahuja82 | last post by:
Hi I am using atoi and strtoul in my code. I have read from MSDN that both functions return 0 if they are unsuccessful. i.e strtoul returns 0 if no conversion can be performed. Now the string...
3
by: pauldepstein | last post by:
The following description of atoi is pasted from cplusplus.com. My question is after the pasting. ***** PASTING BEGINS HERE ****** int atoi ( const char * str ); <cstdlib> Convert string...
11
by: Nezhate | last post by:
Hi all, Can you help me? Why this warning appears in the next simple code ? warning: passing argument 1 of ‘atoi’ makes pointer from integer without a cast. #include <stdio.h> #include...
50
by: Bill Cunningham | last post by:
I have just read atoi() returns no errors. It returns an int though and the value of the int is supposed to be the value of the conversion. It seems to me that right there tells you if there was...
10
by: 66650755 | last post by:
First,thanks for all who have answered my last question. if char string="12345"; how could I convert the string(that is "3") to an int by using atoi? I only want to convert...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.