LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-11-2012, 06:56 AM   #16
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335

Quote:
Originally Posted by bigearsbilly View Post
Here is a slightly cleaned up version.
The condition in this line of code may/will cause grief:
Code:
if(i > ( sizeof array / sizeof (instruction))) {
    ...
    break;
}
The array is sized to contain 1000 elements, which is indexed using values 0 through 999. If the variable 'i' is incremented to 1000, the condition statement above will return 'false', and thus the loop will be executed again.

The condition statement should appear as:
Code:
if(i == ( sizeof array / sizeof (instruction))) {
    ...
    break;
}
Alternatively, but not necessary, the >= comparator may be used.
 
Old 10-11-2012, 03:08 PM   #17
elenizi
LQ Newbie
 
Registered: Oct 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
i did change the points you recommended and found out the problem was a type conflict when using atoi from my input file... i had misunderstood the use of atoi... my general point of view it to get a line of text from an input file and then categorize it and do virtual calculations with what it says for example if it says add i have to add the initial contents of the two variables that follow add but i am having a problem finding a way to read that line save it in a table and then get it back and cut it into pieces to get the instruction and the variables... does anyone have any ideas how to do this? =/
 
Old 10-11-2012, 05:47 PM   #18
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by elenizi View Post
i did change the points you recommended and found out the problem was a type conflict when using atoi from my input file... i had misunderstood the use of atoi... my general point of view it to get a line of text from an input file and then categorize it and do virtual calculations with what it says for example if it says add i have to add the initial contents of the two variables that follow add but i am having a problem finding a way to read that line save it in a table and then get it back and cut it into pieces to get the instruction and the variables... does anyone have any ideas how to do this? =/
atoi() attempts to convert a string commencing with numerical characters into an integer number; it is not very wise to use this function because it will return 0 (zero) if it is unable to deduce a number from the string. And 0, as you know, is a valid number.

When reading raw input, maybe even from a file, use fgets() to get the line of data and then use sscanf() to parse the data. sscanf() will return the number of successful fields it was able to parse. If you are expecting a single number, and sscanf() return 0, well, that's a clue it failed to parse that number.

Anyhow, we (perhaps only I) have no clue as to what your data looks like. Perhaps if you posted your code and a sample of your data file we could assist you more.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] segmentation fault problem?? s_hy Programming 7 03-05-2012 10:59 PM
Segmentation fault problem odedbobi Linux - Software 1 11-20-2008 12:03 PM
Regex.h problem in c++, segmentation fault vargadanis Programming 4 07-14-2008 05:36 PM
problem with segmentation fault lucs Slackware 2 04-28-2005 09:14 AM
Segmentation Fault Problem luvonmik Linux - Newbie 2 02-14-2004 07:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration