LinuxQuestions.org
Review your favorite Linux distribution.
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
 
LinkBack Search this Thread
Old 02-23-2006, 04:11 PM   #1
didier2309
LQ Newbie
 
Registered: Nov 2004
Location: Miami
Distribution: Topologilinux
Posts: 1

Rep: Reputation: 0
Segmentation Fault when executing Program that prints out a number input


I get a segmentation fault when executing this program

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <iostream>
#include <cstdlib>
#include <string.h>

using namespace std;

int main(int argc, char *argv[])
{
int num, rest, len, result;
int control= 0;
char *str;
bool flag = true;
int multi10 = 1;
if(argc == 2){
len = strlen(argv[1]);
}
for(int i = 0; i == len-1 || isdigit(argv[1][i]) ; i++){
flag = false;
}
if(argc != 2 || flag){
cout << "To many or not enough parameters! Enter a number:";
cin >> num;
while(!isdigit(num)){
cout << "\nEnter a number:";
cin >> num;
}
}
else{
num = atoi(argv[1]);
}
if(num > 9){
result = num;
while(result > 9){

result = result/10;
multi10 = multi10 *10;
}
do{
str[control] = result;
rest = num - multi10 * result;
result = rest;
while(result > 9){
result = result/10;
multi10 = multi10 * 10;
}
control++;
}while(result > 0);
cout << str;
return EXIT_SUCCESS;
}
}
 
Old 02-23-2006, 04:55 PM   #2
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
here is one problem..
Code:
char* str;
....
str[control] = result;
i would bet that will cause a segfault.. use something else.. like
char str[SOME_SIZE];
str[control] = result;


and also
code tags!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Program, Segmentation Fault Fireball7 Programming 6 12-07-2005 04:22 PM
why segmentation fault in this program? asahlot Programming 13 10-17-2005 12:47 PM
Program received signal SIGSEGV, Segmentation fault ims_mca Linux - Distributions 0 03-09-2005 04:16 AM
libxml++: segmentation fault when exiting program. isl01jbe Programming 0 06-20-2004 05:47 AM
Executing chat script aolnet.scm Segmentation fault degraffenried13 Linux - Software 0 11-16-2003 08:05 PM


All times are GMT -5. The time now is 07:16 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration