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 09-10-2010, 08:10 AM   #1
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,495

Rep: Reputation: 85
variable not incrementing in c


I am using gcc main.c -o main
It compiles fine, but gets stuck in the loop. z never increments. I was using a for loop originally but had the same problem so I decided to try a while loop.


Code:
/* Includes */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* Constants */
#define MAX_DIGITS 6
#define MAX_INPUT 30

/* Function prototypes */
static void print_one_digit(int d);
static void print_error(char s[]);
static void remove_newline_if_present(char *str);

/* Main program */
int main() {

  printf("Enter numbers in their decimal form.\n");
  printf("To stop, enter a negative value.\n");

//  do {
    char str_num_arr[20]; 
    
    printf("Number: ");
    fgets(str_num_arr, sizeof str_num_arr, stdin);

    remove_newline_if_present(&str_num_arr);


    printf("%s\n", str_num_arr);

    int int_num_arr[20] = {};

    int z = 0;
    while(z<MAX_INPUT) {
      int_num_arr[z] = atoi(&str_num_arr[z]);
      char a = str_num_arr[z];
      printf("%d", int_num_arr[z]);
      z++;
    } 
    printf("does it ever get here?");


//@TODO Add breaks
//    do {

//    } while(1);

//  } while(1);
  return 0;
}

static void print_error(char s[]){ 
  printf("%s", s); abort(1);
}
static void print_one_digit(int d) {
  switch(d) {
    case 0: printf("zero"); break;
    case 1: printf("one"); break;
    case 2: printf("two"); break;
    case 3: printf("three"); break;
    case 4: printf("four"); break;
    case 5: printf("five"); break;
    case 6: printf("six"); break;
    case 7: printf("seven"); break;
    case 8: printf("eight"); break;
    case 9: printf("nine"); break;
    default: print_error("Illegal call to print_one_digit");
  }
}

static void remove_newline_if_present(char *str) {
  int b;
  b = strlen(str)-1;
  if(str[b] == '\n') 
    str[b] = '\0';
}

Last edited by fakie_flip; 09-10-2010 at 08:12 AM.
 
Old 09-10-2010, 08:25 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
Quote:
It compiles fine
You sure? What version GCC? I get:
Code:
a.c: In function ‘main’:
a.c:27: warning: passing argument 1 of ‘remove_newline_if_present’ from incompatible pointer type
a.c: In function ‘print_error’:
a.c:54: error: too many arguments to function ‘abort’
with gcc 4.3, 4.1, and 3.4.
 
Old 09-10-2010, 09:12 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
MAX_INPUT is defined to be 30
Array sizes are 20

You possibly end up with one of the arrays overwriting the data in z which could result in the loop continuing for much longer than you were expecting.

Last edited by graemef; 09-11-2010 at 07:54 PM.
 
Old 09-11-2010, 12:25 PM   #4
imitheos
Member
 
Registered: May 2005
Location: Greece
Posts: 441

Rep: Reputation: 141Reputation: 141
Quote:
Originally Posted by fakie_flip View Post
I am using gcc main.c -o main
It compiles fine, but gets stuck in the loop. z never increments. I was using a for loop originally but had the same problem so I decided to try a while loop.
It is exactly as graemef said. You overwrite the value of z
because you write after the array.

Quote:
Originally Posted by fakie_flip View Post

Code:
    int z = 0;
    while(z<MAX_INPUT) {
Even if you enlarge the array to 30 characters, this check is not correct.
You need to check the length of the string given (for example if i type
the number "50", you check 30 chars when you should check 2).

Quote:
Originally Posted by fakie_flip View Post
Code:
      int_num_arr[z] = atoi(&str_num_arr[z]);
You want to get only one digit, right ? If yes, then this won't work.
You can get the digit easily with
Code:
      int_num_arr[z] = str_num_arr[z] - '0';
 
  


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
Thanked counter is not incrementing jaypas LQ Suggestions & Feedback 5 08-20-2010 05:56 PM
How can I make a self-incrementing variable? OutThere Linux - General 3 04-06-2009 09:12 PM
incrementing variable in an until loop jeffreybluml Programming 2 05-12-2005 07:45 AM
Automaticly incrementing Version Number BoldKiller Programming 7 03-26-2005 12:08 PM
Incrementing volume through amixer mpn Linux - General 1 10-30-2004 08:42 PM

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

All times are GMT -5. The time now is 08:56 PM.

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