I'd like to be able to read a line of text from input, and output on the screen.
please provide sample code.
Thanks.
Printable View
I'd like to be able to read a line of text from input, and output on the screen.
please provide sample code.
Thanks.
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
No. Read a tutorial and do it yourself.
thanks for the advice.
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;
}
sorry..add the following code into the segment above.
char ch;
Which C++ compiler are you using?
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;
}
Thanks.
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.
Well, think about it logically.
In particular, let's review what these two lines do:
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.Code:int intCnt = 0;
ptText = (char *)malloc(intCnt * sizeof(int));
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.
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).Code:intCnt++;
ptText[intCnt] = ch;
In any decent IDE (i.e. not Dev-C++, get Code::Blocks instead) this line is unnecessary.Code:system("PAUSE");
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.
The "return 0;" may be omitted for main() in C++.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";
}
}
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.
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.
VS is fine. Be sure to start your programs with the Run option, not the Debug option.