| | |
Need Help counting Array Length
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
A newbie to C++ here. I need help with counting array length's, I have an array that i only want the user to be able to enter up to ten characters, but lets say if i enter eleven, the array is expanded out to eleven charactes, and i dont want that. i want the array to be cut off at ten.
so far this is the code that i have.
i need to count the array on character at a time in a loop, after each iteration it will check to see what number of the count that it is on. if it is at ten it will simply cut off the end of the array leaving with a nice neat little array. Any help would be greatly appreciated. As to the fact that i am a newbie to C++ any help on better ways to write my code would be much appreciated.
so far this is the code that i have.
C++ Syntax (Toggle Plain Text)
/* Welcome to Dungeon, This will be a simple game */ /* that will require the users name, and confont him */ /* with a maze in which he must navigate! */ /* Good Luck! */ #include <iostream.h> int main() { char UsrName[10]; //will prompt user for name to be used throughout the game cout<<"Welcome to Dungeon!\nPlease enter your name!\nYour name can only be up to 10 characters long.\n"; //write a function to stop array from becoming over ten characters long cin>>UsrName; cout<<"Welcome "<<UsrName<<"!"; }
i need to count the array on character at a time in a loop, after each iteration it will check to see what number of the count that it is on. if it is at ten it will simply cut off the end of the array leaving with a nice neat little array. Any help would be greatly appreciated. As to the fact that i am a newbie to C++ any help on better ways to write my code would be much appreciated.
Dont forget to spread the reputation to those that deserve!
I would recommend getline() instead of cin >>:
Here's more information about getline() and its paramaters.
Hope this helps,
- Stack Overflow
C++ Syntax (Toggle Plain Text)
cin.getline(UsrName, 10, '\n');
Here's more information about getline() and its paramaters.
Hope this helps,
- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.
IRC
Channel: irc.daniweb.com
Room: #c, #shell
IRC
Channel: irc.daniweb.com
Room: #c, #shell
If you're using C++, why aren't you using std::string?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
•
•
Originally Posted by Dave Sinkula
If you're using C++, why aren't you using std::string?
Dont forget to spread the reputation to those that deserve!
•
•
Join Date: Mar 2004
Posts: 1,620
Reputation:
Solved Threads: 51
Hello,
You might want to send your dad an email then
IF Memory serves, you can read the string into a [40] string array (for safety) and then do a string copy of the first 10 characters. Don't forget the 11th character for internal storage of your \n (return) so that things align nicely inside of the program.
Of course, that will not work if you want to error check. You might then use a strlen to see if the array is X size, and then use if-logic to see if people are following directions.
Christian
You might want to send your dad an email then

IF Memory serves, you can read the string into a [40] string array (for safety) and then do a string copy of the first 10 characters. Don't forget the 11th character for internal storage of your \n (return) so that things align nicely inside of the program.
Of course, that will not work if you want to error check. You might then use a strlen to see if the array is X size, and then use if-logic to see if people are following directions.
Christian
Here is the updated code thus far. Im looking for a way to repeat steps in the switch if they enter invalid term for continuing. any help would be greatly appreciated!
C++ Syntax (Toggle Plain Text)
/* Welcome to Dungeon, This will be a simple game */ /* that will require the users name, and confont him */ /* with a maze in which he must navigate! */ /* Good Luck! */ #include <iostream.h> int main() { char UsrName[10]; //will prompt user for name to be used throughout the game cout<<"Welcome to Dungeon!\nPlease enter your name!\nYour name can only be up to 10 characters long.\n"; cin>>UsrName; //write a function to stop array from becoming over ten characters long if (strlen(UsrName) > 10) { do { cout<<"You have entered a name too long!\n"; cin>>UsrName; } while (strlen(UsrName) > 10); } cout<<"Welcome "<<UsrName<<"!"; cout<<"\nYou are about to embark on the shortest god damn maze of your life..\nstraight forward is the exit.\nThere is no left nor right just yet!\nHave fun "<<UsrName<<"!\n"; //lets call us some variable, X will be used to determin ContExit (continue or exit) //if contexit is 1 we continue //if contexit is 0 we exit int x, ContExit; cout<<"\n1 to continue and 0 to exit\n"; cin>>x; if (x == 1) { ContExit = 1; } else { ContExit = 0; } //write a switch statement that will exit or start if exit = 0 or cont = 0 respectivly switch (ContExit) { case 0: cout<<"\nCya Next time!"; return 0; case 1: cout<<"\nYou are faced with one exit:\n1: North\n"; int y; cin>>y; if (y == 1) { cout<<"\nThank you for playing the game"<<UsrName<<"!\n"<<"Hope to see you next time!\n"; return 0; } else { cout<<"\nYou didnt enter a valid Direction!\n"; } } return 0; }
Dont forget to spread the reputation to those that deserve!
Hello again,
I believe I understand what you are looking to do. To further clarify, are you looking to re-ask the question of continuing if not 0 or 1 [invalid] by the users input?
If so there are 2 main ways to accomplish this:
The second is most recommended, though I will explain both.
Using goto
Explaining the goto statement first, lets look at our modifications:
Above the line:
Lets put:
Simple. C provides the infinitely-abusable goto statement. The goto statement labels to branch to. ask here is our scope of a label in the entire function.
Also, instead of setting ContExit = 0; in your else to find x, lets set it to x so we know what was entered. Even if 0 was entered, we would still know about it. Ex:
After this, in your switch statement include the default case. Why? The case labeled default is executed if none of the other cases are satisfied. Exactly what we need! In the case, we tell the compiler to "goto ask;"
Here is a look:
Now when you compile, if the user did not type 0 or 1, it will go back and ask them what they want infinitely until they choose a valid choice.
Splitting your code
This way is more often reliable, and less dependant. This way, we can split sections of your code in seperate functions. Have a look:
I commented our new areas. Most is self-explanatory. Though, lets take a quick look on how this works:
Review
We make two functions outside of main() called getinput() and makechoice().
Next we pull out the input factor and move it into getinput(). getinput() takes two parameters, your choice, and name. Even though the choice hasn't been given yet, we will send the memory address of ContExit to getinput(). This will allow us to change the variable state somewhere else, and if you come back to main() and check ContExit's value, it will be what getinput() set it to last.
After that, we call on makechoice(). makechoice() also takes two parameters, choice and name. This time, we don't send the memory address of our choice to this function because we already know the answer. makechoice() determines what the outcome of the program will do next. 0 exits, 1 moves on, invalid re-asks the question using the default label. We send the memory address of choice in makechoice() back to getinput() for re-evaluation if an invalid choice was received. We also need some way to track your name, so we pass it around all of the functions.
Summary
I do hope this has shed some light on the issue, and can now help you understand more of the C language.
If you have any questions, please feel free to ask. I will try to help you further in any way possible to clearly understand the syntax above.
- Stack Overflow
I believe I understand what you are looking to do. To further clarify, are you looking to re-ask the question of continuing if not 0 or 1 [invalid] by the users input?
If so there are 2 main ways to accomplish this:
- Use the goto statement
- Split your program into different functions
The second is most recommended, though I will explain both.
Using goto
Explaining the goto statement first, lets look at our modifications:
Above the line:
C++ Syntax (Toggle Plain Text)
cout<<"\n1 to continue and 0 to exit\n";
ask:
cout<<"\n1 to continue and 0 to exit\n";Simple. C provides the infinitely-abusable goto statement. The goto statement labels to branch to. ask here is our scope of a label in the entire function.
Also, instead of setting ContExit = 0; in your else to find x, lets set it to x so we know what was entered. Even if 0 was entered, we would still know about it. Ex:
else {
ContExit = x;
}After this, in your switch statement include the default case. Why? The case labeled default is executed if none of the other cases are satisfied. Exactly what we need! In the case, we tell the compiler to "goto ask;"
Here is a look:
switch (ContExit) { case 0: ... case 1: ... default: goto ask; }
Now when you compile, if the user did not type 0 or 1, it will go back and ask them what they want infinitely until they choose a valid choice.
Splitting your code
This way is more often reliable, and less dependant. This way, we can split sections of your code in seperate functions. Have a look:
#include <iostream.h> // Put calls up here so compiler doesn't whine void getinput(int *, char *); // Make this one return int, so return 0 doesn't conflict int makechoice(int, char *); // Your own function seperate from main() for easy call void getinput(int *choice, char *name) { int x; cout<<"\n1 to continue and 0 to exit\n"; cin>>x; if (x == 1) *choice = 1; else *choice = x; makechoice(*choice, name); } // Here is the decision making factor int makechoice(int choice, char *name) { switch (choice) { case 0: cout<<"\nCya Next time!"; return 0; case 1: cout<<"\nYou are faced with one exit:\n1: North\n"; int y; cin>>y; if (y == 1) { cout<<"\nThank you for playing the game"<<name<<"!\n"<<"Hope to see you next time!\n"; return 0; }else { cout<<"\nYou didnt enter a valid Direction!\n"; } default: // call again in case we dont get the results we want getinput(&choice, name); } return 1; } int main() { // Our local variables int ContExit; char UsrName[10]; cout<<"Welcome to Dungeon!\nPlease enter your name!\nYour name can only be up to 10 characters long.\n"; cin >> UsrName; if (strlen(UsrName) > 10) { do { cout<<"You have entered a name too long!\n"; cin>>UsrName; } while (strlen(UsrName) > 10); } cout<<"Welcome "<<UsrName<<"!"; cout<<"\nYou are about to embark on the shortest god damn maze of your life..\nstraight forward is the exit.\nThere is no left nor right just yet!\nHave fun "<<UsrName<<"!\n"; // Call our function here getinput(&ContExit, UsrName); return 0; }
I commented our new areas. Most is self-explanatory. Though, lets take a quick look on how this works:
Review
We make two functions outside of main() called getinput() and makechoice().
Next we pull out the input factor and move it into getinput(). getinput() takes two parameters, your choice, and name. Even though the choice hasn't been given yet, we will send the memory address of ContExit to getinput(). This will allow us to change the variable state somewhere else, and if you come back to main() and check ContExit's value, it will be what getinput() set it to last.
After that, we call on makechoice(). makechoice() also takes two parameters, choice and name. This time, we don't send the memory address of our choice to this function because we already know the answer. makechoice() determines what the outcome of the program will do next. 0 exits, 1 moves on, invalid re-asks the question using the default label. We send the memory address of choice in makechoice() back to getinput() for re-evaluation if an invalid choice was received. We also need some way to track your name, so we pass it around all of the functions.
Summary
I do hope this has shed some light on the issue, and can now help you understand more of the C language.
If you have any questions, please feel free to ask. I will try to help you further in any way possible to clearly understand the syntax above.
- Stack Overflow
Last edited by Stack Overflow; Sep 16th, 2004 at 11:55 pm. Reason: Added Review
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.
IRC
Channel: irc.daniweb.com
Room: #c, #shell
IRC
Channel: irc.daniweb.com
Room: #c, #shell
•
•
•
•
Originally Posted by Stack Overflow
Hello again,
Splitting your code
This way is more often reliable, and less dependant. This way, we can split sections of your code in seperate functions. Have a look:
#include <iostream.h> // Put calls up here so compiler doesn't whine void getinput(int *, char *); // Make this one return int, so return 0 doesn't conflict int makechoice(int, char *); // Your own function seperate from main() for easy call void getinput(int *choice, char *name) { int x; cout<<"\n1 to continue and 0 to exit\n"; cin>>x; if (x == 1) *choice = 1; else *choice = x; makechoice(*choice, name); } // Here is the decision making factor int makechoice(int choice, char *name) { switch (choice) { case 0: cout<<"\nCya Next time!"; return 0; case 1: cout<<"\nYou are faced with one exit:\n1: North\n"; int y; cin>>y; if (y == 1) { cout<<"\nThank you for playing the game"<<name<<"!\n"<<"Hope to see you next time!\n"; return 0; }else { cout<<"\nYou didnt enter a valid Direction!\n"; } default: // call again in case we dont get the results we want getinput(&choice, name); } return 1; } int main() { // Our local variables int ContExit; char UsrName[10]; cout<<"Welcome to Dungeon!\nPlease enter your name!\nYour name can only be up to 10 characters long.\n"; cin >> UsrName; if (strlen(UsrName) > 10) { do { cout<<"You have entered a name too long!\n"; cin>>UsrName; } while (strlen(UsrName) > 10); } cout<<"Welcome "<<UsrName<<"!"; cout<<"\nYou are about to embark on the shortest god damn maze of your life..\nstraight forward is the exit.\nThere is no left nor right just yet!\nHave fun "<<UsrName<<"!\n"; // Call our function here getinput(&ContExit, UsrName); return 0; }
- Stack Overflow
Questions:
1. I dont understand the two void inputs
2. I dont know what the purpose of the *'s are, pointers maybe?
3. I dont understand what the char *name is used for.
any chance that you can explain the different return 'some value' and what they mean do. in the code you say to return into so that return 0 doesnt conflict. so can you explain what would a return 0 incomapred to a return 5 or even a return int is.
((also side note, does anyone know of any good freebased compilers? i am currently using a really old version of borland C++ compiler, but its very ugly, and the syntax coloring is nonexistant at that. I've tried bloodshed, but it doesnt work, and i was hoping for somthing that had smart syntax. so if you start to type int a yellow box will appear with valid forms of int. kinda like dreamweavers ability to do that))
Dont forget to spread the reputation to those that deserve!
![]() |
Other Threads in the C++ Forum
- Previous Thread: problem about statement
- Next Thread: graphics - where to start?
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






