LinuxQuestions.org
Visit Jeremy's Blog.
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 12-22-2002, 11:10 PM   #1
satellite
LQ Newbie
 
Registered: Dec 2002
Location: San Francisco, CA, USA
Distribution: LFS, Splack, Redhat 6.2
Posts: 5

Rep: Reputation: 0
getchar()


Hi everybody. I am just beginning to learn the C language.

I am having a problem with a seemingly simple script, but can't figure out why it's not working.

Code:
/* Read ordinary text a character at a time from the program's
 * stantard input, and print it with each line reversed from left to right.
 * Read until you encounter end-of-data.
 */

#include <stdio.h>  
#include <stdlib.h>    // May not need all of these
#include <string.h>

#define LIMIT 100


main(){

        int string [LIMIT];
        int i=0;
        int q;
        int total;
        for (i=0; i<LIMIT; i++){
                string[i] = getchar();
                if (string[i] == EOF ) {
                total = i;
                break;
                }
        }
        //total = i;
        for ( q=total; q >= 0; q--){
                 putchar(string[q]);
        }
return (0);
}
I've tried a bunch of stuff, and I either get nothing back, or my terminal goes all funky.

Any help would be great. Thanks
 
Old 12-23-2002, 12:22 AM   #2
boku
Member
 
Registered: Aug 2002
Location: Sweden
Distribution: Gentoo
Posts: 43

Rep: Reputation: 15
change:

total = i;

to:

total = i - 1;
 
Old 12-26-2002, 01:09 PM   #3
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 345

Rep: Reputation: 76
One thing I'm missing from that is you don't seem to even test for a new line! I would expect you would do something like...
if (string[i] == '\n') // Aha, a new line.

Be careful though, in the horrible bad world of Micro$oft new lines are a carriage return followed by a linefeed ie. a '\r' followed by '\n'
 
Old 12-20-2017, 01:46 PM   #4
theheapWalker
LQ Newbie
 
Registered: Dec 2017
Posts: 21

Rep: Reputation: Disabled
another suggestion for change is just before return add a final putchar('\n');
so the prompt on the screen is clear of the reverse line of text your routine created.

Code:
      }
        //total = i;
        for ( q=total; q >= 0; q--){
                 putchar(string[q]);
        }
putchar('\n');
return (0);
}
 
Old 12-24-2017, 07:54 AM   #5
rhubarbdog
Member
 
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 132

Rep: Reputation: Disabled
Quote:
Originally Posted by cyent View Post
Be careful though, in the horrible bad world of Micro$oft new lines are a carriage return followed by a linefeed ie. a '\r' followed by '\n'
Not so fopen when used on microsoft automatically strips the \r from the input stream putting them back for a write.
That's why the b-binary exists as an fopen option, useless on linux, but on microsoft leaves \r\n pairs intact
 
Old 12-24-2017, 08:04 AM   #6
rhubarbdog
Member
 
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 132

Rep: Reputation: Disabled
Change/add
Code:
    if(string[i]== EOF) {
      string[i]='\0';
      total=i-1;
      break;
      }
Although it doesn't matter in your example it's good practice to nul terminate your strings
 
Old 12-24-2017, 10:12 AM   #7
rhubarbdog
Member
 
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 132

Rep: Reputation: Disabled
You don't distinguish between no characters read, EOF being reached and 101 characters enteted. In the first and last case total hasn't been set and will lead to problems.
 
Old 12-24-2017, 11:20 AM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,208

Rep: Reputation: 705Reputation: 705Reputation: 705Reputation: 705Reputation: 705Reputation: 705Reputation: 705
Maybe he fixed the code since December 2002, who knows?
 
Old 12-24-2017, 01:04 PM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro
Posts: 6,397

Rep: Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238
this gives yu a means to get out of loop
Code:
/* Read ordinary text a character at a time from the program's
 * stantard input, and print it with each line reversed from left to right.
 * Read until you encounter end-of-data.
 */

#include <stdio.h>  

#define LIMIT 100

int main()
{
	char buffer [LIMIT], c = '\0';
	int i=0;  
        
	printf("enter one letter or number\n"
	       "Then press enter\n"
	       "Enter # to end before 100\n");
	 
	while (c != '#')
	{
		c = getchar();
		buffer[i] = c;
		i++;
	}
	printf("Next\n");
	//i value has already been set
	for ( ; i > 0; i--)
		printf("%c",buffer[i]);
		      
return 0;
}
taking in one at a time, then printing out as if it is a line, is counterintuitive. Unless you read in an entire line then write it (the code) to put it into an array one char at a time, then print out the line backwards, or ...

Code:
char buffer2[100];

printf("enter a string\n");
        
getchar();
fgets(buffer2,sizeof buffer2, stdin);
b = strlen(buffer2);
	
  for (; b > 0; b--)	
	printf("%c",buffer2[b]);
		
	printf("\n");
gets a line of text then prints it out backwards.

Last edited by BW-userx; 12-24-2017 at 01:47 PM.
 
Old 12-24-2017, 01:21 PM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,666
Blog Entries: 6

Rep: Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531
Quote:
Originally Posted by keefaz View Post
Maybe he fixed the code since December 2002, who knows?
Yes, this was a 15-year dead thread before being awakened, let's allow it to return to its repose.
 
1 members found this post helpful.
Old 12-24-2017, 01:47 PM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro
Posts: 6,397

Rep: Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238
grrrr, got duped
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
getchar() question h/w Programming 3 12-23-2003 04:53 PM
about getchar() captainstorm Programming 6 10-11-2003 04:14 AM
getchar() NSKL Programming 18 01-11-2003 10:19 AM
getchar() not working? lackluster Programming 3 06-24-2002 08:09 AM
getchar() Winter Programming 6 05-11-2002 01:49 AM

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

All times are GMT -5. The time now is 03:40 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration