473,548 Members | 2,622 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

New to C please help nice and easy!!

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.

Nov 14 '05 #1
16 1564
ho*********@gma il.com wrote:
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.


#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.l earn.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
Nov 14 '05 #2
"Thomas Matthews" <Th************ *************@s bcglobal.net> wrote in
message news:HQ******** ***********@new ssvr33.news.pro digy.com...
ho*********@gma il.com wrote:
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.


#include <iostream>


That's not C.

<snip>
Nov 14 '05 #3
# include<stdio.h >

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

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

return 0;
}

Nov 14 '05 #4


Thomas Matthews wrote:
ho*********@gma il.com wrote:

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.

#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;
}


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.)

--
Er*********@sun .com

Nov 14 '05 #5


vijay wrote:
# include<stdio.h >

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

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

return 0;
}


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.

--
Er*********@sun .com

Nov 14 '05 #6
Eric Sosman <er*********@su n.com> writes:
[...]
Observation: `do { ... } while (1);' is almost
always a mistake. Conjecture: "always" can be deleted
without falsifying the observation.


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

--
Keith Thompson (The_Other_Keit h) ks***@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.
Nov 14 '05 #7
Thomas Matthews wrote:
ho*********@gma il.com wrote:
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.


#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;
}


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.l earn.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
Nov 14 '05 #8


Keith Thompson wrote:
Eric Sosman <er*********@su n.com> writes:
[...]
Observation: `do { ... } while (1);' is almost
always a mistake. Conjecture: "always" can be deleted
without falsifying the observation.

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


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

--
Er*********@sun .com

Nov 14 '05 #9
wow i posted this not so long ago thanks for the response guys

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1698
by: mark | r | last post by:
ive got down adding editing and deleting items form a database and searching based on criteria... ...how do i set up parent and sub categories (say for a contacts database) i.e. fred smith is an employee of blogs and sons in the sheffield office? so you could drill into a menu via company and then office location (its not what i am doing...
5
1230
by: r2_r2 | last post by:
I am very much a beginner to python. I have been working on writing a very simple program and cannot get it and was hoping someone could help me out. Basically i need to write a code to print a sin curve running down the page from top to bottom. The trick is I have to do this using only 7-8 lines of code. Any ideas?
9
1221
by: Sam Martin | last post by:
naming conventions for C# constants? we're having a mild dispute as to what is better and what is more commonly used? (better = easy to see as constant, nice to look at in code and most consistant with what everyone else is doing) should it be private const string InternalCode = "ABC";
3
1059
by: msnnews.msn.com | last post by:
hi there, i have the following code on my ASPX page..: <LINK id="myCSS" runat="server" href="../print.css" type="text/css" rel="stylesheet" /> how do i reference this in the behind VB code so i can make the HREF dynamic?
6
1761
by: sexygal | last post by:
I need a program to do the following: 1. Access a file where I would have a list of people each with a different number assigned to them. 2. Ask the user to input one of the numbers 3. return the name which is associated with that number I have eclipse, and know I need to do something with scanner and sort the array. But I have no idea...
10
1138
by: =?Utf-8?B?Q2hpV2hpdGVTb3g=?= | last post by:
i just finished creating an asp.net application named teddybears. during the testing phase uploaded live ( ex: www.teddy.com) , i tried to purchase something and add it to my shopping cart. then, i tried opening the application on a different client computer. I just randomly checked the shopping cart and there i found the same items i...
0
7512
marktang
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...
0
7438
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7466
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...
0
6036
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5362
isladogs
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...
0
3495
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...
1
1926
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
0
751
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.