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; } } |
here is one problem..
Code:
char* str; char str[SOME_SIZE]; str[control] = result; and also code tags! :mad: |
All times are GMT -5. The time now is 03:29 AM. |