|
 |
|
 |
11-03-2003, 02:26 PM
|
#1 (permalink)
|
|
Registered User
Join Date: May 2003
Location: Paris, France
Posts: 31
|
need help with arrays
ok... i cant quite figure out what the problem is with this program....
[SHELL]
#include <iostream.h>
#include <stdlib.h>
#include <ctype.h>
long date[3];
int dateValid();
int monthState();
int main()
{
cout << "Please enter a date dd/mm/yyyy: ";
cin >> date[0] >> date[1] >> date [2];
dateValid();
cout << date[1];
monthState();
cout << date[2];
system("PAUSE");
return 0;
}
int dateValid()
{
for (int x=0; x<3; x++)
{
if (isdigit(date[x]))
cout << "Please enter a date dd/mm/yyyy: ";
else
cout << "Entry Valid\n";
};
};
int monthState();
{
int x= date[0];
char month[12] =
{
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"};
cout << month[x];
};
[/SHELL]
and whenever i complile it i get this beautiful little error
Quote:
|
initialization to `char' from `const char *' lacks a cast
|
12 times...
what the heck is going on?
|
|
|
11-03-2003, 03:46 PM
|
#2 (permalink)
|
|
LOAD "*",8,1
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
|
Re: need help with arrays
Quote:
Originally posted by CoW
and whenever i complile it i get this beautiful little error 12 times...
|
12 times, eh?
have you looked at the line number the compiler complains about? your "char month[12]" is a single array of 12 characters, but you're trying to assign to it 12 character arrays;
try:
char *month[12]
|
|
|
11-03-2003, 04:11 PM
|
#3 (permalink)
|
|
Registered User
Join Date: May 2003
Location: Paris, France
Posts: 31
|
ok, no more compile errors (yay!)
but now after dateValid() runs the program crashes....
|
|
|
11-03-2003, 06:03 PM
|
#4 (permalink)
|
|
LOAD "*",8,1
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
|
well, you have several bugs in your program.
your use of isdigit() is incorrect.
your monthState() has an off-by-one error.
i think the way you cin dates is wrong as well, but it's been quite a while since i've used cin, so i'm not sure about it.
try writing a small component of the program at a time. compile, test, fix, test, fix, test, ..., and when you're confident it works, go on to the next part.
|
|
|
11-03-2003, 07:17 PM
|
#5 (permalink)
|
|
Registered User
Join Date: May 2003
Location: Paris, France
Posts: 31
|
Quote:
Originally posted by joe_bruin
well, you have several bugs in your program.
your use of isdigit() is incorrect.
your monthState() has an off-by-one error.
i think the way you cin dates is wrong as well, but it's been quite a while since i've used cin, so i'm not sure about it.
try writing a small component of the program at a time. compile, test, fix, test, fix, test, ..., and when you're confident it works, go on to the next part.
|
feel free to explain how my isdigit() use is wrong, because i can't see how im using it improperly...
as for month state... i cant see anything wrong with it...
my cins are fine.
|
|
|
11-03-2003, 07:42 PM
|
#6 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,505
|
int isdigit() returns a non-zero if true and a zero if false.
either way it returns an integer.
now i'm not sure on this but perhaps test for zero isntead of boolean.
Code:
if (isdigit(date[x]) == 0)
cout << "Please enter a date dd/mm/yyyy: ";
else
cout << "Entry Valid\n";
};
i'm just guessing here based on this documentation: http://www.mkssoftware.com/docs/man3/isdigit.3.asp
__________________
Mike
|
|
|
11-03-2003, 07:45 PM
|
#7 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,505
|
also, after looking at it .. if your code is correct, then it appears your logic is wrong:
Code:
if (isdigit(date[x]))
{
cout << "Please enter a date dd/mm/yyyy: ";
else
cout << "Entry Valid\n";
}; // should there be a ';' at the end of this line?
assuming isdigit would return true/false with that if statement, if it 'is a digit' then it would ask you to please enter a date .. and if it 'is not a digit' then it would say entry valid.
also, that semi colon at the end of the '}' .. are you sure that goes there? i really don't know . just doesnt' look right.
i think it should be more like this:
Code:
if (!isdigit(date[x]))
{
cout << "Please enter a date dd/mm/yyyy: ";
}
else
{
cout << "Entry Valid\n";
}
p.s. i'm no c++ expert .. so take my advice with a grain of salt .. just tryin to help if i can.
__________________
Mike
|
|
|
11-03-2003, 08:02 PM
|
#8 (permalink)
|
|
Registered User
Join Date: May 2003
Location: Paris, France
Posts: 31
|
what the heck... everything compiles ok, and i replaced the isdigit statement with yours and tahts working fine, but the date[2] always returns as "10" in the dateValid() function.... and then the console app crashes..
|
|
|
11-03-2003, 10:45 PM
|
#9 (permalink)
|
|
LOAD "*",8,1
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
|
Quote:
Originally posted by CoW
feel free to explain how my isdigit() use is wrong, because i can't see how im using it improperly...
|
sde is off in this case, your test did work (his correction did, too). but read the documentation on it very carefully, you are not using it correctly. here's a hint from the link sde included:
"isdigit() - tests if character is a digit"
the semicolon at the end of the if statement was also technically wrong, but in this case it did not interfere with anything (it's just an empty statement).
Quote:
|
as for month state... i cant see anything wrong with it...
|
oh? try entering '12' for the month and see what happens. that is, when you fix your cin.
not quite. i'd advice you to look at the values of date[1] and date[2] and figure out why they are incorrect.
|
|
|
11-04-2003, 05:11 AM
|
#10 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,505
|
thanks for checkin that joe  .. maybe i should put permanent disclaimers in my sig 
__________________
Mike
|
|
|
11-04-2003, 05:42 AM
|
#11 (permalink)
|
|
Registered User
Join Date: May 2003
Location: Paris, France
Posts: 31
|
thanks for the help guys, i dicided to drop the isdigit thing all together as it only works for values 1 thru 9...
heres the finished (working!) code.
[SHELL]
#include <iostream.h>
#include <stdlib.h>
#include <ctype.h>
int date[3];
int dateValid();
int monthState();
int main()
{
cout << "Please enter a date dd/mm/yyyy: ";
cin >> date[0];
cin >> date[1];
cin >> date[2];
dateValid();
cout << date[1] << " ";
monthState();
cout << date[2];
system("PAUSE");
return 0;
}
int dateValid()
{
if (date[0] > 31 || date[0] < 1)
cout << "Invalid Entry\n";
else
cout << "Entry Valid\n";
if (date[1] > 12 || date[1] < 1)
cout << "Invalid Entry\n";
else
cout << "Entry Valid\n";
if (date[2] > 2020 || date[2] < 1990)
cout << "Invalid Entry\n";
else
cout << "Entry Valid\n";
};
int monthState()
{
int x= date[0];
char *month[12] = {"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"};
cout << month[x] << " ";
};
[/SHELL]
|
|
|
11-04-2003, 06:29 AM
|
#12 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,505
|
Quote:
Originally posted by sde
thanks for checkin that joe .. maybe i should put permanent disclaimers in my sig
|
haha .. i didn't really think about it but i think my sig is pretty much just that.
__________________
Mike
|
|
|
11-04-2003, 10:09 AM
|
#13 (permalink)
|
|
LOAD "*",8,1
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
|
finished and working? seems to have a bunch of errors in it.
why does it print "February" when i enter "1" for the month?
if i enter "1/1/2000" it still doesn't work.
you have the following warnings.:
test.cpp: In function `int dateValid ()':
test.cpp:43: warning: no return statement in function returning non-void
test.cpp: In function `int monthState ()':
test.cpp:55: warning: no return statement in function returning non-void
those semicolons at the end of your functions (ex "};") don't belong there (like sde pointed out previously).
i don't mean to be hard on you, i'm just telling you what is wrong and hoping you learn by fixing it.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 03:51 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|