Connecting Tech Pros Worldwide Help | Site Map

strange problem

 
LinkBack Thread Tools Search this Thread
  #1  
Old October 20th, 2006, 12:55 PM
Pitaridis Aristotelis
Guest
 
Posts: n/a
Default strange problem

can someone tell me why the following program does not display the text that
have been collected?

#include <stdio.h>

int main()
{
char c, sentence[80];
int i = 0;

puts("Enter a line of text:\n");
while ((c = getchar()) != '\n' );
{
sentence[i] = c;
i++;
}

sentence[i] = '\0'; /* Insert null at end of string */

puts("\nThe line entered was :");
puts(sentence);

return 0;
}


Aristotelis



  #2  
Old October 20th, 2006, 01:05 PM
mlimber
Guest
 
Posts: n/a
Default Re: strange problem

Pitaridis Aristotelis wrote:
Quote:
can someone tell me why the following program does not display the text that
have been collected?
>
#include <stdio.h>
>
int main()
{
char c, sentence[80];
int i = 0;
>
puts("Enter a line of text:\n");
while ((c = getchar()) != '\n' );
That line should not have a semicolon.
Quote:
{
sentence[i] = c;
i++;
}
>
sentence[i] = '\0'; /* Insert null at end of string */
>
puts("\nThe line entered was :");
puts(sentence);
>
return 0;
}
But there are other potential problems. Why aren't you using
std::string instead of raw arrays
(http://www.parashift.com/c++-faq-lit...tml#faq-34.1)?

Cheers! --M

  #3  
Old October 20th, 2006, 01:15 PM
Tim Love
Guest
 
Posts: n/a
Default Re: strange problem

"Pitaridis Aristotelis" <pitaridis@hotmail.comwrites:
Quote:
>can someone tell me why the following program does not display the text that
>have been collected?
Because you've an extra ';' at the end of the 'while' line.

See
http://www-h.eng.cam.ac.uk/help/tpl/...+/FAQ.html#1.1


  #4  
Old October 20th, 2006, 02:55 PM
red floyd
Guest
 
Posts: n/a
Default Re: strange problem

mlimber wrote:
Quote:
Pitaridis Aristotelis wrote:
Quote:
Quote:
> while ((c = getchar()) != '\n' );
>
That line should not have a semicolon.
>
Quote:
> {
> sentence[i] = c;
> i++;
> }
>>
Ah, memories. I made exactly the same mistake in the very first
program I wrote in C (this was 1984) as a professional developer.

for (i = 0 ; i < N; i++);
{
/* initialize some_array[i] */
}

We've all been there, done that.
  #5  
Old October 20th, 2006, 05:35 PM
Stuart Redmann
Guest
 
Posts: n/a
Default Re: strange problem

Pitaridis Aristotelis wrote:
Quote:
can someone tell me why the following program does not display the text that
have been collected?
>
#include <stdio.h>
>
int main()
{
char c, sentence[80];
int i = 0;
>
puts("Enter a line of text:\n");
while ((c = getchar()) != '\n' );
{
sentence[i] = c;
i++;
}
>
sentence[i] = '\0'; /* Insert null at end of string */
>
puts("\nThe line entered was :");
puts(sentence);
>
return 0;
}
1. When posting to newsgroups, please choose a telling header. 'strange
problem' is pretty non-descript, don't you think?

2. Buy yourself a decent compiler. Mine will issue a warning in above cases.

3. Why don't you try to debug your code? Tracing once through it would
have solved this in no time.

Regards,
Stuart
  #6  
Old October 20th, 2006, 05:45 PM
Default User
Guest
 
Posts: n/a
Default Re: strange problem

red floyd wrote:
Quote:
mlimber wrote:
Quote:
Pitaridis Aristotelis wrote:
>
Quote:
Quote:
while ((c = getchar()) != '\n' );
That line should not have a semicolon.
Quote:
Ah, memories. I made exactly the same mistake in the very first
program I wrote in C (this was 1984) as a professional developer.

When I was learning C, I had a sheet of paper with the heading, "Having
Problems Dummy?", where I recorded various gotchas that bit me. Old
Turbo C wasn't as forthcoming with warnings as some of the modern
compilers, so stuff like the above or:

if (y = 1)
{
}


Or not passing pointers to scanf(), used to pass right on through.



Brian

  #7  
Old October 20th, 2006, 06:05 PM
red floyd
Guest
 
Posts: n/a
Default Re: strange problem

Stuart Redmann wrote:
Quote:
Pitaridis Aristotelis wrote:
Quote:
>can someone tell me why the following program does not display the
>text that have been collected?
>>
>#include <stdio.h>
>>
>int main()
>{
> char c, sentence[80];
> int i = 0;
>>
> puts("Enter a line of text:\n");
> while ((c = getchar()) != '\n' );
> {
> sentence[i] = c;
> i++;
> }
>>
> sentence[i] = '\0'; /* Insert null at end of string */
>>
> puts("\nThe line entered was :");
> puts(sentence);
>>
> return 0;
>}
>
1. When posting to newsgroups, please choose a telling header. 'strange
problem' is pretty non-descript, don't you think?
>
2. Buy yourself a decent compiler. Mine will issue a warning in above
cases.
>
3. Why don't you try to debug your code? Tracing once through it would
have solved this in no time.
>
You forgot:

4. Post this to comp.lang.c, as this is pure C, not C++.
  #8  
Old October 20th, 2006, 06:05 PM
jjds101@yahoo.com
Guest
 
Posts: n/a
Default Re: strange problem


Default User wrote:
Quote:
red floyd wrote:
>
Quote:
mlimber wrote:
Quote:
Pitaridis Aristotelis wrote:
Quote:
> while ((c = getchar()) != '\n' );
>
That line should not have a semicolon.
>
Quote:
Ah, memories. I made exactly the same mistake in the very first
program I wrote in C (this was 1984) as a professional developer.
>
>
When I was learning C, I had a sheet of paper with the heading, "Having
Problems Dummy?", where I recorded various gotchas that bit me. Old
Turbo C wasn't as forthcoming with warnings as some of the modern
compilers, so stuff like the above or:
>
if (y = 1)
{
}
>
>
Or not passing pointers to scanf(), used to pass right on through.
>
When I was in college I spent countless hours trying to fix a bug that
I finally traced to something like:

if (some.condition()); {
//of course this block always executed because of that extraneous
semicolon}
}

  #9  
Old October 20th, 2006, 08:05 PM
Pitaridis Aristotelis
Guest
 
Posts: n/a
Default Re: strange problem

You are right about the subject. I am sorry.

I am using the Dev-C++ because I heard that it is the best freeware
environement, but the error explanations are awfull. Can you suggest an
other environement for C++ developement?

Aristotelis

Ï "Tim Love" <tpl@eng.cam.ac.ukÝãñáøå óôï ìÞíõìá
news:ehaj9b$llv$1@gemini.csx.cam.ac.uk...
Quote:
>
"Pitaridis Aristotelis" <pitaridis@hotmail.comwrites:
>
Quote:
>>can someone tell me why the following program does not display the text
>>that
>>have been collected?
Because you've an extra ';' at the end of the 'while' line.
>
See
http://www-h.eng.cam.ac.uk/help/tpl/...+/FAQ.html#1.1
>
>

  #10  
Old October 21st, 2006, 12:05 AM
BobR
Guest
 
Posts: n/a
Default Re: strange problem


Pitaridis Aristotelis wrote in message <1161375406.882341@athprx03>...
Quote:
>You are right about the subject. I am sorry.
>
>I am using the Dev-C++ because I heard that it is the best freeware
>environement, but the error explanations are awfull. Can you suggest an
>other environement for C++ developement?
>
>Aristotelis
PLEASE, do not top-post!!!

The 'error explanations' are NOT awful! YOU must learn!!!

Get "Thinking in C++", 2nd ed. Volume 1 (&2) by Bruce Eckel
(available for free here. You can buy it in hardcopy too.):
http://www.mindview.net/Books/TICPP/...ngInCPP2e.html

alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/

--
Bob R
POVrookie
--
Dev-C++ IDE: http://www.bloodshed.net/
MinGW (GNU compiler): http://www.mingw.org/
MinGWStudio http://www.parinyasoft.com/
wxWidgets URL: http://www.wxwidgets.org
V IDE & V GUI: http://www.objectcentral.com/


 

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.