|
-
Sep 29th, 2006, 04:00 PM
#1
Thread Starter
Lively Member
read a line of text
I'd like to be able to read a line of text from input, and output on the screen.
please provide sample code.
Thanks.
-
Sep 29th, 2006, 05:21 PM
#2
Thread Starter
Lively Member
Re: read a line of text
I am sorry, I wasn't very clear. I am trying to read a line of text (can be anything, any length....."lasdkqjflkajsdlkjfla"), and then output each of the character(l,a,s,d,k,q.......l,a).
I am using Unix environment. Also, as you can, I am very new with C/C++. Please provide sample code.
Thanks
-
Sep 29th, 2006, 08:48 PM
#3
Re: read a line of text
No. Read a tutorial and do it yourself.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 29th, 2006, 09:32 PM
#4
Lively Member
-
Oct 1st, 2006, 01:20 AM
#5
Lively Member
Re: read a line of text
Can someone please look at my code and tell me what's wrong as it is not runing properly.
This segment of code suppose to read a (Undetermined lenght) line of text from input, store into an array, and output it using the array. What I am trying to do here is using malloc so that I can have unlimited space to store the string of text.
I have spent hours trying to figure out this, but still have no success. Please help.
Thanks.
main()
{
char *ptText;//pointer
int intCnt = 0;//is a number of character in the array
ptText = malloc(intCnt * sizeof(int)); //allocate the memory
printf("Please enter your text: \n");
ch = getchar();
while(ch !='\n')
{
intCnt++;
ptText[intCnt] = ch;
printf("You have entered: \%c\n\", ptText[intCnt]);
}
return 0;
}
-
Oct 1st, 2006, 01:23 AM
#6
Lively Member
Re: read a line of text
sorry..add the following code into the segment above.
char ch;
-
Oct 1st, 2006, 02:52 AM
#7
Re: read a line of text
Which C++ compiler are you using?
-
Oct 1st, 2006, 03:40 AM
#8
Junior Member
Re: read a line of text
Fixed your code, it works now. And you really need a better IDE/Compiler that can check your code and tell you what the problem is. The only thing I'm unsure about is the use of malloc (I don't use it).
Code:
#include <cstdio>
#include <cstdlib>
using namespace std;
main()
{
char ch;
char *ptText;
int intCnt = 0;
ptText = (char *)malloc(intCnt * sizeof(int));
printf("Please enter your text: \n");
ch = getchar();
while(ch !='\n')
{
intCnt++;
ptText[intCnt] = ch;
printf("You have entered: %c\n", ptText[intCnt]);
ch = getchar();
}
system("PAUSE");
return 0;
}
Last edited by Chazwazza; Oct 1st, 2006 at 03:49 AM.
-
Oct 1st, 2006, 10:46 AM
#9
Lively Member
-
Oct 1st, 2006, 12:37 PM
#10
Lively Member
Re: read a line of text
dont know why, but it is still not working. when I compiled, no error message. But I when run it, all it does is produce the line "Please enter your text: ". I tried to put in some text, nothing after that.
Thanks.
-
Oct 1st, 2006, 01:39 PM
#11
Re: read a line of text
Well, think about it logically.
In particular, let's review what these two lines do:
Code:
int intCnt = 0;
ptText = (char *)malloc(intCnt * sizeof(int));
The first sets the variable intCnt to zero. The second allocates some memory. How much? Well, it allocates sizeof(int) times the value of intCnt bytes. Now, there are two errors in here. The first is that you're allocating memory for a sequence of characters, so why the sizeof(int)? You want sizeof(char), but that is defined by the standard to be 1, so you can omit it.
The second is that the value of intCnt at this point is guaranteed to be 0 (you just assigned the value on the previous line - for all intents and purposes, you're passing a constant), which means that the result of the multiplication is also 0. You're allocating 0 bytes, no memory at all.
And then you try to write into it. Bad idea.
Code:
intCnt++;
ptText[intCnt] = ch;
Then there's this little error. Indexing in C and C++ is 0-based, so by incrementing intCnt before using it you never write to the first byte you allocated (or not).
In any decent IDE (i.e. not Dev-C++, get Code::Blocks instead) this line is unnecessary.
Now, let's rewrite the program so that it actually works. First, we will switch from C to C++ since your initial post indicated that you don't care either way (you probably just stumbled over a C tutorial first) and so we'll use the easier language, that hides all that ugly memory management from you. Newbies never do well on that.
To switch to C++, you need to end the source file in .cpp instead of .c. And of course use C++ facilities, namely cin and cout instead of printf and getchar.
Code:
#include <iostream>
#include <string>
int main()
{
std::string line; // Look, no pointers.
std::cout << "Please enter your text:\n";
std::getline(std::cin, line); // Look, no loop.
std::cout << "You entered: " << line << '\n';
std::cout << "Or as individual characters ...\n";
int count = line.size();
for(int i = 0; i < count; ++i) {
std::cout << "Character #" << (i + 1) << ": '" << line[i] << "'\n";
}
}
The "return 0;" may be omitted for main() in C++.
Other than that, the code should be pretty clear. cout and cin and objects representing standard output and input (usually screen and keyboard) of your application, respectively. You can "stuff" things into cout with the << operator. The getline call does exactly what it says: it reads a line from the object passed as the first argument and puts it into the string passed as the second argument. The string object 'line' really behaves like a string is supposed to. The std:: prefix indicates that those things are part of the C++ standard library.
That should get you started.
PS: If you're wondering why I only gave you an RTFM in the first post and now post an elaborate reply, complete with code, the reason is simple: this time I can see that you've made an effort.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 2nd, 2006, 02:48 AM
#12
Junior Member
Re: read a line of text
Thanks CornedBee, I noticed that malloc was being used incorectly, I just didn't know how to do it, I don't use it. Also, I'll get Code::Blocks now, I'm using VS 2003, 2005 and Dev C++, I'll give Code::Blocks a go.
-
Oct 2nd, 2006, 02:51 AM
#13
Re: read a line of text
VS is fine. Be sure to start your programs with the Run option, not the Debug option.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|