|
-
November 24th, 2009, 05:05 PM
#1
Resetting variables
Hey all 
Ok the situation is that for a college assinemnt i am required to create an application that calculates whether or not the user is allowed credit based on three variables, age, income and hours of work.....now these are set as variables and i have several functions.
The thing I am wanting to do is set them to a Null length when the function has finished as at the moment the user cant enter variables for the first attempt, but when the second attempt comes it skips all of the while loops (while(variable==0)) which isnt what i want, im wanting it to request the variables again, these are char variables.
The reason for me using while loops is because my lecturer gave me the evils when using the goto command >.> any help would be appreciated thanks
PS - wasnt going to post all of code as it is all complete, just touching up and dont want to spam the forum with it, feel free to be technical......programming is my love and joy
-
November 24th, 2009, 07:24 PM
#2
Re: Resetting variables
But you're more than welcome to post your code snippet in [code] [/code] tags.
-
November 24th, 2009, 07:40 PM
#3
Re: Resetting variables
I'm a little confused by the description but it sounds like you should just set them to 0 at the end of your loop, as it sounds like your code might be within one. But I'm just guessing.
-
November 24th, 2009, 08:38 PM
#4
Re: Resetting variables
 Originally Posted by hirsty
...three variables, age, income and hours of work.....
...these are char variables.
Really? How do you stuff income into a char variable? (Must not be too much of an income)
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
November 25th, 2009, 01:08 AM
#5
Re: Resetting variables
ive tried resetting them using the end of the variabe last i remember, tried an inbuilt reset function, the way in which i set a value to them is using atoi(variable) = integer
-
November 25th, 2009, 01:22 AM
#6
Re: Resetting variables
Please provide code example.
Also explain what happens and what you would like to happen.
Before post, make an effort yourself, try googling or search here.
When posting, give a proper description of your problem, include code* and error messages.
*All code should include code tags
-
November 25th, 2009, 01:56 AM
#7
Re: Resetting variables
think i have just found my solution Thanks for offering to help to divide and conquer, on calling back to the Menu from the function I have used memset(variable,0,sizeof(variable))
-
November 25th, 2009, 02:02 AM
#8
Re: Resetting variables
unlesss there is an easier then feel free t oadvise currently this is my code for my menu and the other function:
void Menu()
{
int mchoice;
system("cls");
while(mchoice!=3)
{
memset(birthyr, 0, sizeof(birthyr)); // resets char variable to 0
memset(income, 0, sizeof(income));
memset(wrkhrs, 0, sizeof(wrkhrs));
choice='/0';
credit=0;
cout<<"Welcome to the Working Tax Credit Calculator. In order to process your enquiry"<<endl;
cout<<"further we need to know whether you are enquiring as a couple. Please choose "<<endl;
cout<<"from the following options:"<<endl<<endl;
cout<<"1. Enquiry for Singles"<<endl;
cout<<"2. Enquiry for Couples"<<endl;
cout<<"3. Exit"<<endl<<endl;
cout<<"Menu choice: ";
cin>>mchoice;
cout<<endl;
if (mchoice==1)
..........
else
cout<<"Please enter a valid menu choice"<<endl<<endl;
}
}
void SingleEnq()
{
while((atoi(birthyr)) == 0)
{
cout<<"Please enter your year of birth(YYYY): ";
cin>>birthyr;
if((atoi(birthyr))==0)
{
cout<<"Birth Year is not valid"<<endl;
cin.clear();
}
.....continues to check variables in same way, then asks if correct, if so carrys on to calculate.....
}
what was happening, on second entry of the SingleEnq() it was using the previous choices and i did not want that to happen
-
November 25th, 2009, 08:18 AM
#9
Re: Resetting variables
Why are you using char arrays to enter numeric data?
Instead of memsetting your arrays, you could change your while to a do/while.
Verifying the year of birth using atoi isn't particularly robust. 99934CD will convert to a non-zero value but it isn't a valid year of birth.
-
November 25th, 2009, 10:59 AM
#10
Re: Resetting variables
 Originally Posted by GCDEF
Why are you using char arrays to enter numeric data?
Instead of memsetting your arrays, you could change your while to a do/while.
Verifying the year of birth using atoi isn't particularly robust. 99934CD will convert to a non-zero value but it isn't a valid year of birth.
Well, I am a college student and basically googled how to do it as i was wanting to do nnumeric calculations on it as well as display it to the screen, ive done VB but only just started coding C++ so yea found this and applied it as like a qucik solution...-shrugs-
-
November 25th, 2009, 11:25 AM
#11
Re: Resetting variables
 Originally Posted by hirsty
Well, I am a college student and basically googled how to do it as i was wanting to do nnumeric calculations on it as well as display it to the screen, ive done VB but only just started coding C++ so yea found this and applied it as like a qucik solution...-shrugs-
If you're working with numeric data, you should use numeric data types.
-
November 25th, 2009, 11:27 AM
#12
Re: Resetting variables
 Originally Posted by GCDEF
If you're working with numeric data, you should use numeric data types.
how would i then detect if text has been input and how would i limit the amount of characters??? (wanting to make fool proof)
-
November 25th, 2009, 11:47 AM
#13
Re: Resetting variables
 Originally Posted by hirsty
how would i then detect if text has been input and how would i limit the amount of characters??? (wanting to make fool proof)
The std iostreams come with operator<< and operator>>, which basically take the input, and convert it to the corresponding numeric tipe, like this:
This will even work if your input is something like 0.0f, or an hex integer, provided you told your stream you are reading an hex (it can't guess for you)
As for checking for text, if the user types "apple", then the read will "fail" (this is normal), and you will be able to tell your user to try again.
Why would you want to limit the amount of characters? If your user needs 278 characters to input his value, who are you to say it is wrong? And if it is, you should just handle the fault, then tell your user to try again.
Once you are done reading and validating user input, you are 100% guaranteed to have perfectly valid input. It is part of standard methods to never do anything before you have validated the objects you are working with. You can code away without worries 
std iostream s are standard, expandable, error tolerant, and safe. atoi is non of those things.
-
November 25th, 2009, 11:57 AM
#14
Re: Resetting variables
 Originally Posted by hirsty
how would i then detect if text has been input and how would i limit the amount of characters??? (wanting to make fool proof)
Typically that would be done something like this....
Code:
string strval;
int val;
cin >> strval;
istringstream valstream(strval);
if (!(valstream >> val))
cout << "bad input";
The problem with simply testing "cin >> val" directly is that if it fails, the bad input will remain in the stream, and you'll continue to fail until you either .ignore() it or you try to read something from cin which is not an integer.
(Using cin.ignore() is another option.)
-
November 25th, 2009, 12:09 PM
#15
Re: Resetting variables
I wouldn't use operator >>.
Learn how to grab input 1 line at a time as a string, and then parse from the string what you need using stringstreams. This is the C++ version of the old fgets/sscanf combo used by C programmers. It sounds a lot more complicated than it is and there are many examples on these boards. A simple search will throw up a few results.
This is such a valuable technique that its well worth learning earlier rather than later imo. It also avoids nicely the problem of mixing calls to getline and operator >> which is fraught with trouble.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|