Archived

This topic is now archived and is closed to further replies.

Please help with C++ code!

This topic is 5146 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am learning C++ and trying to write a console program that will check a number if it is valid.... If someone can help steer me in the right direction I'm sure I could figure out the rest of the error checking functions, as it is now I just cant get it to return a different value if the number is invalid no matter what i do!! Also I know some of the loops may be a bit sloppy, I have been experamenting with a few varities, in the last function I was tryign to do it with a 'if' most of the time, just changed it to a while before I posted here to see if on som eoff chance that would work... it didnt Thanks in advance! Sorry its not in a script box, not sure how to do it... Ill try /script but it will prolly mak eme look like a ass EDIT: i updated this so the source is in the right box

#include <fstream>
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;
void verify(char[12]);
int caseone (char);
void dirs();

void main ()
{
	char correct = 'n';
	char test[12];
	int more = 5;
	int num = 5;
cout << caseone(num) << "\nTHIS IS IN MAIN CASEONE RETURN!\n\n";
while (more == 5)
{

	dirs();
	cin >> test;

	verify(test);
	cout << caseone(num) << "\nTHIS IS IN MAIN CASEONE RETURN!\n\n";
	more = caseone(num);
}

cout << "done!";
}

void dirs()
{
	cout << "******************************************************************\n";

	cout << "Please enter a valid book code: \n ";
}
 
void verify (char book[12])
{
	char time = book[0];
	char timetwo =book[9];
	
cout << "ONE\n";//REMOVE THIS... USED TO FIND LOOP ERROR

	caseone(time);
	cout << "TWO\n";//REMOVE THIS... USED TO FIND LOOP ERROR

	caseone(timetwo);
	cout << "THREE\n";//REMOVE THIS... USED TO FIND LOOP ERROR


}
	
int caseone(char num)
{
	//system ("CLS");

	int exit= 1;
		
	while (num == '-' )
	{	
	cout <<endl;
	cout << "INVALID NUMBER!\n ";
	cout << "You cannot start or end with a - \n\n\n";
	exit = 5;
	num = 'a';
	}

	

		return exit;



}
[source]

<SPAN CLASS=editedby>[edited by - justaddwater on November 10, 2003 10:06:59 PM]</SPAN>
[edited by - justaddwater on November 10, 2003 10:09:26 PM]

Share this post


Link to post
Share on other sites
When I step through your code and then run it, I get exactly the expected results, Its hard to tell exactly what you are trying to accomplish.

Please could you be a little more specific about what you want the code to do.

Share this post


Link to post
Share on other sites
Thanks for your help,

I am tryign to get it to loop back to the directions and prompt you to enter a new code if the code is invalid, or continue on out of the loop if it''s valid. (in the program so far that would only result in cout << done!)

The problems I have is it can tell if the code is valid or invalid, based on the ''-'' but it seems to loop a extra time. Also I can''t get it too return a value to main that will shut off the while loop and move on, with a valid answer, or loop back through with a invalid. It seems that whatever value I assin stays the same regardless of what I tell it too return.

I hope that was clear enough, Im 4 weeks into this class, so its kinda hard to explain what Im thinking.

Not to mention after 5 years of working in Sales... argh my mind is so polluted!

Share this post


Link to post
Share on other sites
quote:
Original post by Justaddwater
Anyone know a better website for asking C++ questions? I guess this one isnt really setup for that.


Well I don''t know where everybody else is but I needed my beauty sleep.

If nobody has answered your questions by then, I will take a look at it over lunch and get back to you.

Share this post


Link to post
Share on other sites
Ok, I looked over your code and it looks like your problem is just down to confusion.

Words of advice:

1) Try to take it a step at a time, if you have managed to get a user prompt to the screen then the next thing is to get the program to accept input, don't try to skip ahead to the result.

2) Don't poke at it with a stick. This saying is used when you constantly try to fix the code by adding more and making little changes, instead of sitting back and thinking it through.

They are both things that we have all been guilty of at one time or another.



Ok now for your code, when I went through your code I found that you have quite a lot of code in there that doesn't need to be there and some of your variable and function naming was a little off, so I shaved it down to what I felt you were trying to accomplish and I changed some wording.

#include <iostream>
#include <conio.h> //this is the header for the getch() function


void YaySuccess(); //this function is where you would say what happens next in the porogram, but for now it just exits


using namespace std;

void main()
{

char answer;

do
{
cout << "******************************************************************\n";

cout << "Please enter a valid book code: \n ";


cin >> answer;


if(answer != '-')
{
YaySuccess();
getch(); //this function waits for a keypress to exit the program

}
else
cout << "INVALID CODE\n\n";

}while (answer == '-');

return;

}


void YaySuccess()
{
cout << "yay success!";
}


It keeps asking for a valid book code until a valid one is entered.
If a valid book code is entered, the program jumps to the "YaySuccess" function where in this case, it waits for a keypress and then exits.

This program will work properly if the user enters one character only, if the "book code" is a string of numbers then the code will need to be modified.

If it seems like I have missed out important chunks of your code, just let me know.

[edited by - tiffany_smith on November 11, 2003 5:01:54 PM]

Share this post


Link to post
Share on other sites
Hi,
First off, I was NOT intending to sound pissy about no one answering, or anything like that, I was just afraid after I slept on it that it may have been a bit forward to throw my C++ problems on a gameing website, and if there was a better place for it, then I would do that. Sorry for any confusion.

Second, thanks very much for your help Tiffany, and others. In regards to your corrected code, that works great, the only problem is the bookcode will be 10 numbers and I will be checking various things on the numbers (will they have '-' at end, or start, will they have alph letters etc etc) SO I was trying to take the bookcode in as a char array, then use it that way. But I se you put a header for a getch() function... if you have time and don't mind could you briefly explain how that would work? Thanks

Spudder... Thats a question I have as well, I have the function as a INT and it taking a CHAR, so my understanding was that it would be able to return a INT, but I couldnt get that too work at all.

Yeah there was lots of excess code, I was putting in lines all over the place trying to see what order things were happening in. Had I slept on it before I posted it too the board, I would have though to get it back to the best I was able to, as far as function.

Long winded! Thanks again... now maybe i can sleep more then 2 hours tonite!



[edited by - justaddwater on November 11, 2003 12:07:10 AM]

Share this post


Link to post
Share on other sites
If you''re just looking to see the order of things happening it''s much easier to run your program through the debugger line by line. If you aren''t familiar with the debugger then it''s a good idea to get familiar with it as knowing how to use the debugger is an invaluable skill.





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
quote:
Original post by Justaddwater
Second, thanks very much for your help Tiffany, and others. In regards to your corrected code, that works great, the only problem is the bookcode will be 10 numbers and I will be checking various things on the numbers (will they have '-' at end, or start, will they have alph letters etc etc) SO I was trying to take the bookcode in as a char array, then use it that way.


To check to see if you have a '-' anywhere in the inputted string, try using strtok(), it can be found in the string.h header file.
By using strtok() you can have the bookcode as a char array (a string) and strtok() will search the string for the token (which in this case is '-').

quote:
Original post by Justaddwater
But I se you put a header for a getch() function... if you have time and don't mind could you briefly explain how that would work? Thanks



In the example I wrote, getch() has absolutely nothing to do with the inputted string, in this case it is simply a function that detects any user input. I put it there so that I could see the result on screen before the program quickly terminated.

Try taking it out, figure out what it does.
Here is a reference.

I'm sure you realise that since you have stated that this is homework, I cant give you a full example but hopefully the little pointers will help.

Good luck

[edited by - tiffany_smith on November 12, 2003 1:14:41 AM]

Share this post


Link to post
Share on other sites
Cool deal,

No I don't want the answer in black and white anyway.. what you gave me is perfect! No point paying to go to college and copy pasteing someone else's code into my complier!

Thanks again, lots of stuff to play with now!

Greatwolf... thanks for that info as well, so far all I know of the debugger is there is a button on VS.net that says debugger.. me thinks I should look into that as well

[edited by - justaddwater on November 12, 2003 3:41:17 PM]

Share this post


Link to post
Share on other sites