Hi,
I'm a newbie programmer. I can't get work the following code.
/*
Objective:
Use a loop to print out all of the input characters until a newline is
found.
*/
#include <stdio.h>
int main (){
/* Declare variable, Assign a initial value x. */
char a = 'x';
while (a != '\n') {
/* Get a user input */
a = getchar();
if (a == '\n'){
/* If the input is a new line, say good bye to user */
printf("\nThat' s it for today.\n");
} else {
/* Prints a user input */
printf("%c",a);
}
}
return 1;
} 14 3085
On 6ÔÂ1ÈÕ, ÏÂÎç5ʱ25·Ö, Samuel.Codd...@ gmail.com wrote:
Hi,
I'm a newbie programmer. I can't get work the following code.
/*
Objective:
Use a loop to print out all of the input characters until a newline is
found.
*/
#include <stdio.h>
int main (){
/* Declare variable, Assign a initial value x. */
char a = 'x';
while (a != '\n') {
/* Get a user input */
a = getchar();
if (a == '\n'){
/* If the input is a new line, say good bye to user */
printf("\nThat' s it for today.\n");
} else {
/* Prints a user input */
printf("%c",a);
}
}
return 1;
}
It's my program:
#include <stdio.h>
int main (){
char a;
while ((a = getchar()) != '\n') {
printf("%c",a);
}
printf("\nThat' s it for today.\n");
return 1;
}
right??????
On 1 Jun, 10:25, Samuel.Codd...@ gmail.com wrote:
Hi,
I'm a newbie programmer. I can't get work the following code.
"I can't get work" doesn't tell me much about your problem.
Generally if there's a problem you need help with, it's helpful to
tell us
* What did you expect to happen?
* What did happen?
Your code works for me, and behaves exactly as I expect, but that's
not necessarily what you expected, so you probably need to tell us
what you expected.
I'll give you a hint - standard input is probably line-buffered...
>
/*
Objective:
Use a loop to print out all of the input characters until a newline is
found.
*/
#include <stdio.h>
int main (){
/* Declare variable, Assign a initial value x. */
char a = 'x';
while (a != '\n') {
/* Get a user input */
a = getchar();
if (a == '\n'){
/* If the input is a new line, say good bye to user */
printf("\nThat' s it for today.\n");
} else {
/* Prints a user input */
printf("%c",a);
}
}
return 1;
}
Sa************@ gmail.com wrote:
Minor notes (since Mark has started the major ones):
#include <stdio.h>
int main (){
/* Declare variable, Assign a initial value x. */
Pointless comment.
char a = 'x';
while (a != '\n') {
/* Get a user input */
Ditto.
a = getchar();
`getchar` returns `int`. So `a` should be `int`. (This is because
the result of `getchar` has to allow for every character /and/
EOF.)
(fx:snip)
return 1;
Not portable. 0, EXIT_SUCCESS, EXIT_FAILURE (those from stdlib)
are portable.
}
--
"- born in the lab under strict supervision -", - Magenta, /Genetesis/
Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England
Hi, Tak,
Your code is much clearner. But the result on my end is the same.
I wanna make the program so that the program prints all the inputs
until a user input a new like (generally by pressing return).
So, it should open for a new input.
That string "That's it for today" should be displayed only when a user
type a newline.
Tak said:
It's my program:
#include <stdio.h>
int main (){
char a;
while ((a = getchar()) != '\n') {
printf("%c",a);
}
printf("\nThat' s it for today.\n");
return 1;
}
right??????
Wrong. Well, you're right that it's your program, but you're wrong that
it's right. It's wrong.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
On 1 Jun, 10:53, mark_blue...@po box.com wrote:
On 1 Jun, 10:25, Samuel.Codd...@ gmail.com wrote:
Hi,
I'm a newbie programmer. I can't get work the following code.
"I can't get work" doesn't tell me much about your problem.
Generally if there's a problem you need help with, it's helpful to
tell us
* What did you expect to happen?
* What did happen?
Your code works for me, and behaves exactly as I expect, but that's
not necessarily what you expected, so you probably need to tell us
what you expected.
[Snip]
I'd better qualify my comments by saying that I didn't bother with the
nitpicking about whether you should declare main as "int main(void)"
or that a should be an int. ma**********@po box.com said:
On 1 Jun, 10:25, Samuel.Codd...@ gmail.com wrote:
>Hi,
I'm a newbie programmer. I can't get work the following code.
"I can't get work" doesn't tell me much about your problem.
Actually, I think it tells you all you need to know.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www. Sa************@ gmail.com said:
Hi,
I'm a newbie programmer. I can't get work the following code.
Here's your program again, properly indented for readability:
#include <stdio.h>
int main()
{
/* Declare variable, Assign a initial value x. */
char a = 'x';
while(a != '\n')
{
/* Get a user input */
a = getchar();
if(a == '\n')
{
/* If the input is a new line, say good bye to user */
printf("\nThat' s it for today.\n");
}
else
{
/* Prints a user input */
printf("%c", a);
}
}
return 1;
}
Problems:
1) a has a lousy name!
2) a has the wrong type - it should be int.
Whilst I wouldn't want to suggest that your program is otherwise devoid
of problems, it does appear to do what is asked of it, except in cases
where it hits the end-of-file, where it does get a bit messy, but you
presumably aren't worried about that.
For the record, here's how I'd have written it:
#include <stdio.h>
int main(void)
{
int ch = 0;
while((ch = getchar()) != EOF && ch != '\n')
{
putchar(ch);
}
printf("\nThat' s it for today.\n");
return 0;
}
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
On 1 Jun, 11:27, Richard Heathfield <r...@see.sig.i nvalidwrote:
mark_blue...@po box.com said:
On 1 Jun, 10:25, Samuel.Codd...@ gmail.com wrote:
Hi,
I'm a newbie programmer. I can't get work the following code.
"I can't get work" doesn't tell me much about your problem.
Actually, I think it tells you all you need to know.
Meow! Harsh but essentially fair, I guess.
However, he did post a complete, compilable, relatively cleanly
formatted program - give him some points for that... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Toby Newman |
last post by:
At the page:
http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000
or
http://tinyurl.com/4ptzs
the author warns:
"The for loop is frequently used, usually where the loop will be
traversed a fixed number of times. It is very flexible, and novice
programmers should take care not to abuse the power it offers."
|
by: Redduck |
last post by:
Hello everyone. I am frustrated, I have written the simple program
below for a class and I am having problems with the DO-WHILE loop. On
the first run through the loop, everything works well, the menu is
displayed, the input is registered and the loop runs. On the second
(and following) runs the menu is printed twice. I am sure there is
something very basic that I am missing, but I cannot see it. Can
anyone help?
Thanks in advance.
|
by: Tweaxor |
last post by:
This has been puzzling me all this week. This is actually a homework
assignment
from last semesmter. But the teacher wouldn't tell us why certain
things didn't work, but it didn't just work. My thing was what
actually turn this while loop into an endless loop instead of waiting
for user response it'll would skip right over it. Could someone with
the time explain this to me what would make it behave like this
int pts1, pts2, pts3,...
|
by: JS |
last post by:
#include <stdio.h>
main(){
int c, i, nwhite, nother;
int ndigit;
nwhite = nother = 0;
for (i = 0; i < 10; ++i)
|
by: morpheus |
last post by:
Hi Group,
May I ask the following?
for (i=0; (c=getchar()) != EOF && c != '\n'; ++i)
line=c;
What I am finding is that when c == '\n' is true, the loop is
terminated but i is still incremented. I was under the impression that
termination of the loop should not increment i.
| |
by: Roy Smith |
last post by:
The following code appears to be illegal:
while ((int c = getchar()) != EOF) {
putchar (c);
}
I tried it on two different compilers (Sun workshop and gcc), and both
give some variation on syntax error at "int c =". The very similar:
for (int c = getchar(); c != EOF; c = getchar()) {
|
by: Alan |
last post by:
I am using Standard C compiled with GCC under Linux Fedora Core 4
When I run this program and enter a character at the prompt, I have to press
the ENTER key as well. This gives me 2 input characters - 'a' and
'\n' (Hex 61 and 0a)
It seems as though the getchar() function needs ENTER to terminate reading
stdin.
I am trying to get the program to respond when I press one key only (ie
|
by: tesh.uk |
last post by:
Hi Gurus,
I have written the following code with the help of Ivor Horton's
Beginning C :
// Structures, Arrays of Structures.
#include "stdafx.h"
#include "stdio.h"
#define MY_ARRAY 15
|
by: arnuld |
last post by:
Mostly when I want to take input from stdin I use getchar() but I get this
from man page itself:
"If the integer value returned by getchar() is stored into a variable of
type char and then compared against the integer constant EOF, the
comparison may never succeed, because sign-extension of a variable of type
char on widening to integer is implementation-defined"
while( EOF != (ch = getchar()) ) ....
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |