Programming exercise....
Hi,
I am reading this book c++ interactive course from Robert Lafore and in chapter 4: functions, he gives us an exercise after the one of the lessons. This lesson states that I must make a function which works like the cin.getline() function in the iostream header file. I can only use getche() function in my custom function. I can't seem to be able to get it to work. Can someone help me?
BTW, Please be sure to use simple strings (ex. char str[]) and also no pointer stuff since I'm way before that.
Thanks
[edited by - xtrex on January 3, 2003 10:40:39 PM]
Well here's what you're wanting to do, you can write the code easy, I'll just give you a few helpful tips.
1. Since you don't know pointers yet, just create a char array (which is a pointer...hmmm) of maybe 80 characters (that's about the width of a single line.) Also create a single char, which will be gotten with getche, and an integer which will keep track of the current number of letters. Initialize the integer to 0 with something like:
int i = 0;
2. Create a loop which will get a single char with getche(). Then assign that character to element i (or whatever you called your integer) of the character array. Then increment the integer, and if it is equal to 80, or whatever you set the length of the char array to, then exit the loop.
Then just print out your character array and you will have made a function just like getline!
If you get stuck and just want to see the function I wrote to write this post, just give a yell.
EDIT:
As a note which I forgot, you will want to only increment your integer to 78, and then set element 79 to '\0'
'\0' is a null-terminator character, which means that when you print out your string or use most string functions on it, when the function reaches a '\0' it will know it's at the end of the string. If your string does not end with the null-terminator, then when you print it out you will get some random garbled characters at the end.
aut viam inveniam aut faciam
MoonStar Projects
[edited by - Ronin Magus on January 3, 2003 11:15:29 PM]
1. Since you don't know pointers yet, just create a char array (which is a pointer...hmmm) of maybe 80 characters (that's about the width of a single line.) Also create a single char, which will be gotten with getche, and an integer which will keep track of the current number of letters. Initialize the integer to 0 with something like:
int i = 0;
2. Create a loop which will get a single char with getche(). Then assign that character to element i (or whatever you called your integer) of the character array. Then increment the integer, and if it is equal to 80, or whatever you set the length of the char array to, then exit the loop.
Then just print out your character array and you will have made a function just like getline!
If you get stuck and just want to see the function I wrote to write this post, just give a yell.
EDIT:
As a note which I forgot, you will want to only increment your integer to 78, and then set element 79 to '\0'
'\0' is a null-terminator character, which means that when you print out your string or use most string functions on it, when the function reaches a '\0' it will know it's at the end of the string. If your string does not end with the null-terminator, then when you print it out you will get some random garbled characters at the end.
aut viam inveniam aut faciam
MoonStar Projects
[edited by - Ronin Magus on January 3, 2003 11:15:29 PM]
thanks for the tips. Just some problems: The getline function contains 3 parameters. So the first is where to input the string. Second is how many max characters is will be. Third is defualt argument is a char which basically says stop taking more characters if the user hits a key (for example the enter key). Now I''m guessing the prototype function would be sort of like this:
void getline(char& str[],int max,char terminate = ''\r'')
This maybe wrong so please help me out. The reason i put first parameter as reference so that i can access that char string which is outside the function.
void getline(char& str[],int max,char terminate = ''\r'')
This maybe wrong so please help me out. The reason i put first parameter as reference so that i can access that char string which is outside the function.
Alright, so instead of creating a char array of 80 in your function, just use the array sent in as the first parameter.
And instead of checking if "i < 80" in your loop, use "i < max".
Then, after each time you get a character, check to see if it is the terminate character. If it is, just exit the loop, and add a null-terminator to the end of whatever it is you have.
Edit:
Also remember, arrays are always sent by reference (because they're just pointers.) So you don't need to return anything, anything you do to char[] str in function GetLine will be done to the string array that you sent to it when you called it.
So saying "char &str[]" is a little confusing and overkill, just "char str[]" works.
aut viam inveniam aut faciam
MoonStar Projects
[edited by - Ronin Magus on January 3, 2003 11:24:42 PM]
[edited by - Ronin Magus on January 3, 2003 11:25:07 PM]
And instead of checking if "i < 80" in your loop, use "i < max".
Then, after each time you get a character, check to see if it is the terminate character. If it is, just exit the loop, and add a null-terminator to the end of whatever it is you have.
Edit:
Also remember, arrays are always sent by reference (because they're just pointers.) So you don't need to return anything, anything you do to char[] str in function GetLine will be done to the string array that you sent to it when you called it.
So saying "char &str[]" is a little confusing and overkill, just "char str[]" works.
aut viam inveniam aut faciam
MoonStar Projects
[edited by - Ronin Magus on January 3, 2003 11:24:42 PM]
[edited by - Ronin Magus on January 3, 2003 11:25:07 PM]
Ok, I tried doing that but I get a truck load of errors: I'll show you my code:
EDIT:
Ok I don't get any errors but it still doesn't work.
[edited by - xtrex on January 3, 2003 11:27:56 PM]
[edited by - xtrex on January 3, 2003 11:28:24 PM]
void getlineuser(char str[],int max,char terminate = '\n'){ char ch; int n = 0; while((ch != terminate) && (n < max)) { ch = getche(); str[n++] = ch; }}
EDIT:
Ok I don't get any errors but it still doesn't work.
[edited by - xtrex on January 3, 2003 11:27:56 PM]
[edited by - xtrex on January 3, 2003 11:28:24 PM]
1. Don't give terminate a value in the function declaration. Instead, just say "void getlineuser(char str[], int max, char terminate)" This is because we want to give terminate a value when we call it... like this:
char myChar[80];
getuserline(myChar, 80, '\n');
2. Because you're checking for c != terminate in your while loop. This isn't a technical error, just a logical one. You will always have the terminate char at the end of your string, and with '\n' it's not bad, but it's not what you want in every case. What if you wanted the char '_' to be the terminate char? Here's the desired results:
Input a string: asdf_
You entered: asdf
Instead, you will get this:
Input a string: asdf_
You entered: asdf_
To fix this you need to check if "c == terminate" inside the loop, and if that is true use the "break;" keyword to exit the while loop. "break;" exits out of any for, while, or do-while loop.
3. You're not adding the null terminator char to the end of the string. You would get results like this:
Input a string: asdf
You entered: asdf!_@A**
With just random chars at the end of what you want, because when it prints out the string it doesn't know where to stop and goes off into system memory, getting undesired results.
Edit: spelling errors
Don't worry man, strings may be hard to pick up on (because they're pointers and you don't know pointers yet.) But they won't always be this hard. Once you get a GOOD grip on C++, you can use the STL, which has a string class that's very easy to use. But it's important you know how to use c-style strings.
aut viam inveniam aut faciam
MoonStar Projects
[edited by - Ronin Magus on January 3, 2003 11:55:17 PM]
char myChar[80];
getuserline(myChar, 80, '\n');
2. Because you're checking for c != terminate in your while loop. This isn't a technical error, just a logical one. You will always have the terminate char at the end of your string, and with '\n' it's not bad, but it's not what you want in every case. What if you wanted the char '_' to be the terminate char? Here's the desired results:
Input a string: asdf_
You entered: asdf
Instead, you will get this:
Input a string: asdf_
You entered: asdf_
To fix this you need to check if "c == terminate" inside the loop, and if that is true use the "break;" keyword to exit the while loop. "break;" exits out of any for, while, or do-while loop.
3. You're not adding the null terminator char to the end of the string. You would get results like this:
Input a string: asdf
You entered: asdf!_@A**
With just random chars at the end of what you want, because when it prints out the string it doesn't know where to stop and goes off into system memory, getting undesired results.
Edit: spelling errors
Don't worry man, strings may be hard to pick up on (because they're pointers and you don't know pointers yet.) But they won't always be this hard. Once you get a GOOD grip on C++, you can use the STL, which has a string class that's very easy to use. But it's important you know how to use c-style strings.
aut viam inveniam aut faciam
MoonStar Projects
[edited by - Ronin Magus on January 3, 2003 11:55:17 PM]
ok... here is my code:
its like the same as before with minor changes. The reason I have to give terminate a value is to make it a default argument. So if the user does not supply the third argument the function will automatically use '\n' as the terminate character. This function still doesn't work so I hope you can help me further. I know I'm kind of dumb
Edit:
It's the book that says I should make that terminate parameter a default one.
[edited by - xtrex on January 3, 2003 12:02:02 AM]
void getlineuser(char str[],int max,char terminate='\n'){ char ch; int n = 0; while((ch != terminate) && (n < (max-1))) { ch = getche(); if(ch == terminate) break; str[n++] = ch; } str[n] = '\0';}
its like the same as before with minor changes. The reason I have to give terminate a value is to make it a default argument. So if the user does not supply the third argument the function will automatically use '\n' as the terminate character. This function still doesn't work so I hope you can help me further. I know I'm kind of dumb
Edit:
It's the book that says I should make that terminate parameter a default one.
[edited by - xtrex on January 3, 2003 12:02:02 AM]
Wow.. You learn something every day! I honestly didn't know that you could make a function parameter set like that, and I've been using them for years! Guess I should have really read those books a little better..
Anyways, your code is almost identical to mine, and it seems mine works fine in all the tests I ran. So check it out:
EDIT:
Ahhhh... I see the problem. You're using the '\n' character. I may have told you wrongly earlier, I'm too tired to look In windows, every time the enter key is hit, two characters are sent: "\r\n". The \r means carriage return. The \n means newline. You need to check for '\r' instead of '\n'.
aut viam inveniam aut faciam
MoonStar Projects
[edited by - Ronin Magus on January 3, 2003 12:12:09 AM]
Anyways, your code is almost identical to mine, and it seems mine works fine in all the tests I ran. So check it out:
#include <iostream.h>#include <conio.h>void GetLine(char myChar[], int max, char terminator){ char c; int i = 0; while(i < max) { c = getche(); if(c == terminator) break; myChar[i++] = c; } myChar[i] = '\0';}int main(){ char myChar[80]; GetLine(myChar, 80, '\r'); cout << endl << myChar << endl; return 0;}
EDIT:
Ahhhh... I see the problem. You're using the '\n' character. I may have told you wrongly earlier, I'm too tired to look In windows, every time the enter key is hit, two characters are sent: "\r\n". The \r means carriage return. The \n means newline. You need to check for '\r' instead of '\n'.
aut viam inveniam aut faciam
MoonStar Projects
[edited by - Ronin Magus on January 3, 2003 12:12:09 AM]
hehe... The default argument thing is pretty useful i think.
Anyways my function is working properly now. Before I was using '\n' to terminate but instead I wanted to use the enter key '\r'. So now it works properly even if I don't supply the third argument.
Anyways thanks for the help
Edit:
Just saw your edit. Yes i figured to use '\r' instead of '\n' by looking at your code.
heres my final anyways:
[edited by - xtrex on January 3, 2003 12:23:36 AM]
Anyways my function is working properly now. Before I was using '\n' to terminate but instead I wanted to use the enter key '\r'. So now it works properly even if I don't supply the third argument.
Anyways thanks for the help
Edit:
Just saw your edit. Yes i figured to use '\r' instead of '\n' by looking at your code.
heres my final anyways:
void getlineuser(char str[],int max,char terminate='\r'){ char ch; int n = 0; while((ch != terminate) && (n < (max-1))) { ch = getche(); if(ch == terminate) break; str[n++] = ch; } str[n] = '\0';}
[edited by - xtrex on January 3, 2003 12:23:36 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement