4 Replies - 263 Views - Last Post: 24 January 2013 - 01:06 PM Rate Topic: ***** 1 Votes

#1 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Why ~ Appers At Output?

Posted 24 January 2013 - 09:37 AM

I know ~ is my compilers way to represent something but what is it? I was told to write a function that would clean up newlines, tabs and spaces from a given line. here's what I wrote:

#include <stdio.h>
#define MAXLINE 1000
int getline(char[]);
int main()
{
    char line[MAXLINE];
    getline(line);
    int i = 0;
    while(line[i] != '\0')
    {
        printf("%c", line[i]);
        i++;
    }
    return 0;
}
int getline(char line[])
{
    int i = 0;
    while(i < MAXLINE && scanf("%c", &line[i]) == 1)
    {
        if(line[i] == ' ' || line[i] == '\n' || line[i] =='\t')
        {
            i--;
        }
        i++;
    }
    line[i] == '\0';
    return 0;
}


My code runs perfectly except for the ~ it prints?
Posted Image
Why does it do so?
Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Why ~ Appers At Output?

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,247
  • Joined: 31-December 10

Re: Why ~ Appers At Output?

Posted 24 January 2013 - 09:42 AM

What exactly do you mean by cleanup? Completely remove all whitespace? Why read in a character at a time? Look at line 27 and tell me whats wrong with it? line[i] == '\0';

Your problem is with how you handle newlines. getline should only read in a line, so when you hit \n, you really only need to break out of the loop. Once out of the loop, setting line[i] = '\0' replaces the \n with the NULL character.

This post has been edited by vividexstance: 24 January 2013 - 09:45 AM

Was This Post Helpful? 1
  • +
  • -

#3 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: Why ~ Appers At Output?

Posted 24 January 2013 - 11:07 AM

Got it right...
#include <stdio.h>
#define MAXLINE 1000
int getPara(char[], int);
int main()
{
    char line[MAXLINE];
    getPara(line, MAXLINE);
    printf("%s", line);
    return 0;
}
int getPara(char line[], int lim)
{
    int c, i;
    for(i = 0; i < lim - 1 && (c = getchar()) != EOF ; i++)
    {
        if(c != ' ' && c != '\n' && c != '\t')
        {
            line[i] = c;
        }
        else
        {
            i--;
        }
    }
    line[i] = '\0';
    return 0;
}


This post has been edited by Jeet.in: 24 January 2013 - 11:10 AM

Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,247
  • Joined: 31-December 10

Re: Why ~ Appers At Output?

Posted 24 January 2013 - 01:02 PM

You didn't really need to change your original code that much. This is what I did to figure your problem out:
#include <stdio.h>
#define MAXLINE 1000
int getline2(char[]);
int main()
{
    char line[MAXLINE];
    getline2(line);
    int i = 0;
    while(line[i] != '\0')
    {
        printf("%c", line[i]);
        i++;
    }
    return 0;
}
int getline2(char line[])
{
    int i = 0;
    while(i < MAXLINE && scanf("%c", &line[i]) == 1)
    {
        if(line[i] == ' ' || line[i] =='\t')
        {
            i--;
        }
        else if(line[i] == '\n')
        {
        	break;
        }
        
        i++;
    }
    line[i] = '\0';
    return 0;
}


Was This Post Helpful? 1
  • +
  • -

#5 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: Why ~ Appers At Output?

Posted 24 January 2013 - 01:06 PM

Thanks vividexstance I just wanted to reformat my code K&R style that's why I changed it :).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1