Need Help counting Array Length

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Need Help counting Array Length

 
0
  #1
Sep 15th, 2004
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.

  1. /* Welcome to Dungeon, This will be a simple game */
  2. /* that will require the users name, and confont him */
  3. /* with a maze in which he must navigate! */
  4. /* Good Luck! */
  5. #include <iostream.h>
  6. int main()
  7. {
  8. char UsrName[10];
  9. //will prompt user for name to be used throughout the game
  10. cout<<"Welcome to Dungeon!\nPlease enter your name!\nYour name can only be up to 10 characters long.\n";
  11. //write a function to stop array from becoming over ten characters long
  12. cin>>UsrName;
  13. cout<<"Welcome "<<UsrName<<"!";
  14. }

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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: Need Help counting Array Length

 
1
  #2
Sep 16th, 2004
I would recommend getline() instead of cin >>:

  1. 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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Need Help counting Array Length

 
0
  #3
Sep 16th, 2004
looks good, ill take a looksie into it.
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Need Help counting Array Length

 
0
  #4
Sep 16th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Need Help counting Array Length

 
0
  #5
Sep 16th, 2004
Originally Posted by Dave Sinkula
If you're using C++, why aren't you using std::string?
because honestly im very new to it, and since my dad works from about 9 in the morning untill 11 at night, i dont get chances to ask him very often. Which is why your going to see alot of posts asking for help from mee
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 1,620
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Solved Threads: 51
Team Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: Need Help counting Array Length

 
0
  #6
Sep 16th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Need Help counting Array Length

 
0
  #7
Sep 16th, 2004
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!

  1. /* Welcome to Dungeon, This will be a simple game */
  2. /* that will require the users name, and confont him */
  3. /* with a maze in which he must navigate! */
  4. /* Good Luck! */
  5. #include <iostream.h>
  6. int main()
  7. {
  8. char UsrName[10];
  9. //will prompt user for name to be used throughout the game
  10. cout<<"Welcome to Dungeon!\nPlease enter your name!\nYour name can only be up to 10 characters long.\n";
  11. cin>>UsrName;
  12. //write a function to stop array from becoming over ten characters long
  13. if (strlen(UsrName) > 10)
  14. {
  15. do
  16. {
  17. cout<<"You have entered a name too long!\n";
  18. cin>>UsrName;
  19. }
  20. while (strlen(UsrName) > 10);
  21. }
  22. cout<<"Welcome "<<UsrName<<"!";
  23. 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";
  24. //lets call us some variable, X will be used to determin ContExit (continue or exit)
  25. //if contexit is 1 we continue
  26. //if contexit is 0 we exit
  27. int x, ContExit;
  28. cout<<"\n1 to continue and 0 to exit\n";
  29. cin>>x;
  30. if (x == 1)
  31. {
  32. ContExit = 1;
  33. }
  34. else
  35. {
  36. ContExit = 0;
  37. }
  38. //write a switch statement that will exit or start if exit = 0 or cont = 0 respectivly
  39. switch (ContExit)
  40. {
  41. case 0: cout<<"\nCya Next time!";
  42. return 0;
  43. case 1: cout<<"\nYou are faced with one exit:\n1: North\n";
  44. int y;
  45. cin>>y;
  46. if (y == 1)
  47. {
  48. cout<<"\nThank you for playing the game"<<UsrName<<"!\n"<<"Hope to see you next time!\n";
  49. return 0;
  50. }
  51. else
  52. {
  53. cout<<"\nYou didnt enter a valid Direction!\n";
  54. }
  55. }
  56. return 0;
  57. }
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: Need Help counting Array Length

 
0
  #8
Sep 16th, 2004
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:
  • 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:
  1. cout<<"\n1 to continue and 0 to exit\n";
Lets put:
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Need Help counting Array Length

 
0
  #9
Sep 17th, 2004
Wow that was all very very good input, give me a little time to go over it and see whats with what, so i can write up some questions and post them looking for answers!
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Need Help counting Array Length

 
0
  #10
Sep 17th, 2004
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
what are the *'s in the makechoice and such? what do they do, are they pointers? or do they serve some other purpose? since i am new to this, i am very lost as to alot of the syntax of it all.

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!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC