Connecting Tech Pros Worldwide Forums | Help | Site Map

New to C please help nice and easy!!

hoggmeister@gmail.com
Guest
 
Posts: n/a
#1: Nov 14 '05
Hi,

Im new to C coming from a java background. I having difficulty
adjusting to C and was hoping someone could help me with a little
simple code to get started. I would like a little program that outputs
on to the console a message like "please enter some text" then when the
user enters text it gets stored in a char array or whatever is best. I
then want to check that the array is no longer than 25 chars ( i dont
know if malloc is required or not ) and if it is less than 25 it gets
stored in virtual memory if not then it loops and asks for input again.
Just a nice easy one so i can put in debugger to understand how the
code works. Thanks for looking.


Thomas Matthews
Guest
 
Posts: n/a
#2: Nov 14 '05

re: New to C please help nice and easy!!


hoggmeister@gmail.com wrote:
[color=blue]
> Hi,
>
> Im new to C coming from a java background. I having difficulty
> adjusting to C and was hoping someone could help me with a little
> simple code to get started. I would like a little program that outputs
> on to the console a message like "please enter some text" then when the
> user enters text it gets stored in a char array or whatever is best. I
> then want to check that the array is no longer than 25 chars ( i dont
> know if malloc is required or not ) and if it is less than 25 it gets
> stored in virtual memory if not then it loops and asks for input again.
> Just a nice easy one so i can put in debugger to understand how the
> code works. Thanks for looking.
>[/color]

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(void)
{
string data;
static vector<string> virtual_memory;
while (true)
{
cout << "please enter some text";
getline(cin, data);
if (data.length() >= 25)
{
virtual_memory.push_back(data);
}
} // End: while true
return 0;
}



--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
osmium
Guest
 
Posts: n/a
#3: Nov 14 '05

re: New to C please help nice and easy!!


"Thomas Matthews" <Thomas_MatthewsSpamBotsSuck@sbcglobal.net> wrote in
message news:HQj_d.21776$hU7.13366@newssvr33.news.prodigy. com...[color=blue]
> hoggmeister@gmail.com wrote:
>[color=green]
>> Hi,
>>
>> Im new to C coming from a java background. I having difficulty
>> adjusting to C and was hoping someone could help me with a little
>> simple code to get started. I would like a little program that outputs
>> on to the console a message like "please enter some text" then when the
>> user enters text it gets stored in a char array or whatever is best. I
>> then want to check that the array is no longer than 25 chars ( i dont
>> know if malloc is required or not ) and if it is less than 25 it gets
>> stored in virtual memory if not then it loops and asks for input again.
>> Just a nice easy one so i can put in debugger to understand how the
>> code works. Thanks for looking.
>>[/color]
>
> #include <iostream>[/color]

That's not C.

<snip>


vijay
Guest
 
Posts: n/a
#4: Nov 14 '05

re: New to C please help nice and easy!!


# include<stdio.h>

int main()
{
char str[100];
do{
printf("Enter text: \n");
fgets(str,sizeof(str),stdin);

if(strlen(str)>=25)
continue;
else{
printf("\nThe text is: %s",str);
break;
}
}
while(1);

return 0;
}

Eric Sosman
Guest
 
Posts: n/a
#5: Nov 14 '05

re: New to C please help nice and easy!!




Thomas Matthews wrote:[color=blue]
> hoggmeister@gmail.com wrote:
>
>[color=green]
>>Hi,
>>
>>Im new to C coming from a java background. I having difficulty
>>adjusting to C and was hoping someone could help me with a little
>>simple code to get started. I would like a little program that outputs
>>on to the console a message like "please enter some text" then when the
>>user enters text it gets stored in a char array or whatever is best. I
>>then want to check that the array is no longer than 25 chars ( i dont
>>know if malloc is required or not ) and if it is less than 25 it gets
>>stored in virtual memory if not then it loops and asks for input again.
>> Just a nice easy one so i can put in debugger to understand how the
>>code works. Thanks for looking.
>>[/color]
>
>
> #include <iostream>
> #include <string>
> #include <vector>
> using namespace std;
>
> int main(void)
> {
> string data;
> static vector<string> virtual_memory;
> while (true)
> {
> cout << "please enter some text";
> getline(cin, data);
> if (data.length() >= 25)
> {
> virtual_memory.push_back(data);
> }
> } // End: while true
> return 0;
> }[/color]

I'm having trouble getting this code to work; can
you tell me what's wrong? Here's what the compiler says:

bogus.c:1:20: iostream: No such file or directory (ENOENT)
bogus.c:2:18: string: No such file or directory (ENOENT)
bogus.c:3:18: vector: No such file or directory (ENOENT)
bogus.c:4: parse error before "namespace"
bogus.c:4: warning: type defaults to `int' in declaration of `std'
bogus.c:4: ISO C forbids data definition with no type or storage class
bogus.c: In function `main':
bogus.c:8: `string' undeclared (first use in this function)
bogus.c:8: (Each undeclared identifier is reported only once
bogus.c:8: for each function it appears in.)
bogus.c:8: parse error before "data"
bogus.c:9: warning: ISO C forbids nested functions
bogus.c:9: syntax error before '<' token
bogus.c:10: `true' undeclared (first use in this function)
bogus.c:12: `cout' undeclared (first use in this function)
bogus.c:13: warning: implicit declaration of function `getline'
bogus.c:13: `cin' undeclared (first use in this function)
bogus.c:13: `data' undeclared (first use in this function)
bogus.c:16: `virtual_memory' undeclared (first use in this function)
bogus.c:18: parse error before '/' token
bogus.c:12: warning: statement with no effect
bogus.c:20: warning: control reaches end of non-void function

(An oddity: The lines of diagnostic messages outnumber
the lines of source code.)

--
Eric.Sosman@sun.com

Eric Sosman
Guest
 
Posts: n/a
#6: Nov 14 '05

re: New to C please help nice and easy!!




vijay wrote:[color=blue]
> # include<stdio.h>
>
> int main()
> {
> char str[100];
> do{
> printf("Enter text: \n");
> fgets(str,sizeof(str),stdin);
>
> if(strlen(str)>=25)
> continue;
> else{
> printf("\nThe text is: %s",str);
> break;
> }
> }
> while(1);
>
> return 0;
> }[/color]

This works (or will, after #include <string.h> is
added), but why make the control flow so baroque?

do {
printf (...);
fgets (...);
} while (strlen(str) >= 25);
printf ("The text is: %s\n", str);

.... seems to express the intent both more clearly and
more succinctly.

Observation: `do { ... } while (1);' is almost
always a mistake. Conjecture: "always" can be deleted
without falsifying the observation.

--
Eric.Sosman@sun.com

Keith Thompson
Guest
 
Posts: n/a
#7: Nov 14 '05

re: New to C please help nice and easy!!


Eric Sosman <eric.sosman@sun.com> writes:
[...][color=blue]
> Observation: `do { ... } while (1);' is almost
> always a mistake. Conjecture: "always" can be deleted
> without falsifying the observation.[/color]

Conjecture: You meant that "almost" can be deleted without falsifying
the observation (unless you meant "almost a mistake").

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Thomas Matthews
Guest
 
Posts: n/a
#8: Nov 14 '05

re: New to C please help nice and easy!!


Thomas Matthews wrote:
[color=blue]
> hoggmeister@gmail.com wrote:
>[color=green]
>> Hi,
>>
>> Im new to C coming from a java background. I having difficulty
>> adjusting to C and was hoping someone could help me with a little
>> simple code to get started. I would like a little program that outputs
>> on to the console a message like "please enter some text" then when the
>> user enters text it gets stored in a char array or whatever is best. I
>> then want to check that the array is no longer than 25 chars ( i dont
>> know if malloc is required or not ) and if it is less than 25 it gets
>> stored in virtual memory if not then it loops and asks for input again.
>> Just a nice easy one so i can put in debugger to understand how the
>> code works. Thanks for looking.
>>[/color]
>
> #include <iostream>
> #include <string>
> #include <vector>
> using namespace std;
>
> int main(void)
> {
> string data;
> static vector<string> virtual_memory;
> while (true)
> {
> cout << "please enter some text";
> getline(cin, data);
> if (data.length() >= 25)
> {
> virtual_memory.push_back(data);
> }
> } // End: while true
> return 0;
> }[/color]

Sorry, brain in wrong gear.
{Having to switch between C, C++, Visual Basic, and
VBScript today. Ouch.}


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Eric Sosman
Guest
 
Posts: n/a
#9: Nov 14 '05

re: New to C please help nice and easy!!




Keith Thompson wrote:[color=blue]
> Eric Sosman <eric.sosman@sun.com> writes:
> [...]
>[color=green]
>> Observation: `do { ... } while (1);' is almost
>>always a mistake. Conjecture: "always" can be deleted
>>without falsifying the observation.[/color]
>
>
> Conjecture: You meant that "almost" can be deleted without falsifying
> the observation (unless you meant "almost a mistake").[/color]

In my own high-falutin' syntax I seem to myself
have uptripped. That'll larn me -- 'cept it probably
won't, alas ...

--
Eric.Sosman@sun.com

hoggmeister@gmail.com
Guest
 
Posts: n/a
#10: Nov 14 '05

re: New to C please help nice and easy!!


wow i posted this not so long ago thanks for the response guys

CBFalconer
Guest
 
Posts: n/a
#11: Nov 14 '05

re: New to C please help nice and easy!!


Thomas Matthews wrote:[color=blue]
> hoggmeister@gmail.com wrote:
>[color=green]
>> Im new to C coming from a java background. I having difficulty
>> adjusting to C and was hoping someone could help me with a little
>> simple code to get started. I would like a little program that outputs
>> on to the console a message like "please enter some text" then when the
>> user enters text it gets stored in a char array or whatever is best. I
>> then want to check that the array is no longer than 25 chars ( i dont
>> know if malloc is required or not ) and if it is less than 25 it gets
>> stored in virtual memory if not then it loops and asks for input again.
>> Just a nice easy one so i can put in debugger to understand how the
>> code works. Thanks for looking.[/color]
>
> #include <iostream>
> #include <string>
> #include <vector>
> using namespace std;[/color]

Please don't post C++ code in c.l.c. That is a different language.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


Richard Bos
Guest
 
Posts: n/a
#12: Nov 14 '05

re: New to C please help nice and easy!!


Eric Sosman <eric.sosman@sun.com> wrote:
[color=blue]
> Observation: `do { ... } while (1);' is almost
> always a mistake. Conjecture: "always" can be deleted
> without falsifying the observation.[/color]

If s/"always"/"almost"/, then yes, it can always be replaced by while
(1) { ... }, since the only difference between those two is in when the
condition is evaluated; and in these cases, the condition has no side
effects and is always true, so it doesn't matter when it's evaluated.
Since while is clearer than do...while, it is to be preferred.

Observation: this is only true for normal code. For intentionally
unclear code, such as found in the IOCCC, for example, other rules
apply.

Richard
Tor Rustad
Guest
 
Posts: n/a
#13: Nov 14 '05

re: New to C please help nice and easy!!


<hoggmeister@gmail.com> wrote in message[color=blue]
> Hi,
>
> Im new to C coming from a java background. I having difficulty
> adjusting to C and was hoping someone could help me with a little
> simple code to get started. I would like a little program that[/color]
outputs[color=blue]
> on to the console a message like "please enter some text" then when[/color]
the[color=blue]
> user enters text it gets stored in a char array or whatever is best.[/color]
I[color=blue]
> then want to check that the array is no longer than 25 chars ( i dont
> know if malloc is required or not ) and if it is less than 25 it gets
> stored in virtual memory if not then it loops and asks for input[/color]
again.[color=blue]
> Just a nice easy one so i can put in debugger to understand how the
> code works. Thanks for looking.[/color]

Get a copy of K&R2, there you will find a lot of simple and useful
examples.

--
Tor <torust AT online DOT no>
"C is not a big language, and it is not well served in a big
book". --K&R2

AlexB
Guest
 
Posts: n/a
#14: Nov 14 '05

re: New to C please help nice and easy!!


On Thu, 17 Mar 2005 10:10:10 -0800, hoggmeister wrote:
[color=blue]
> Hi,
>
> Im new to C coming from a java background.[/color]

One of the best things you can do is pick up a
"Teach yourself C in 21 days" book.. It will give you
a good crash course in C. Then pick up a good reference
manual.

-Alex
Ben Pfaff
Guest
 
Posts: n/a
#15: Nov 14 '05

re: New to C please help nice and easy!!


AlexB <BartonekDragRacing@yahoo.com> writes:
[color=blue]
> One of the best things you can do is pick up a
> "Teach yourself C in 21 days" book.. It will give you
> a good crash course in C.[/color]

I haven't heard anyone recommend any such book in clc before.
--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms
CBFalconer
Guest
 
Posts: n/a
#16: Nov 14 '05

re: New to C please help nice and easy!!


AlexB wrote:[color=blue]
> On Thu, 17 Mar 2005 10:10:10 -0800, hoggmeister wrote:
>[color=green]
> > Im new to C coming from a java background.[/color]
>
> One of the best things you can do is pick up a "Teach yourself C
> in 21 days" book.. It will give you a good crash course in C.
> Then pick up a good reference manual.[/color]

Correction, substitute worst for best above. Simply get the
accurate and reliable "The C Programming Language" by Kernighan and
Ritchie, and go to it. It will also function adequately as a
reference manual.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


Tor Rustad
Guest
 
Posts: n/a
#17: Nov 14 '05

re: New to C please help nice and easy!!


"AlexB" <BartonekDragRacing@yahoo.com> wrote in message[color=blue]
> On Thu, 17 Mar 2005 10:10:10 -0800, hoggmeister wrote:
>[color=green]
> > Hi,
> >
> > Im new to C coming from a java background.[/color]
>
> One of the best things you can do is pick up a
> "Teach yourself C in 21 days" book..[/color]

What a bad advice... lol.

K&R2 is one of the best programming books around
(regardless of language) -- try it yourself! :-)

--
Tor <torust AT online DOT no>
"All novices have trouble with this aspect of linking until the concept
is explained. Then they just have trouble with the concept itself." PvdL

Closed Thread