Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 02-23-2006, 05:11 PM   #1
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

#include <config.h>

#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;
cout << "\nEnter a number:";
cin >> num;
num = atoi(argv[1]);
if(num > 9){
result = num;
while(result > 9){

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

Rep: Reputation: 45
here is one problem..
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!


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

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

All times are GMT -5. The time now is 12:05 AM.

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