Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 11-03-2003, 02:26 PM   #1 (permalink)
CoW
Registered User
 
CoW's Avatar
 
Join Date: May 2003
Location: Paris, France
Posts: 31
CoW is on a distinguished road
Send a message via AIM to CoW
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?
CoW is offline   Reply With Quote
Old 11-03-2003, 03:46 PM   #2 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
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]
joe_bruin is offline   Reply With Quote
Old 11-03-2003, 04:11 PM   #3 (permalink)
CoW
Registered User
 
CoW's Avatar
 
Join Date: May 2003
Location: Paris, France
Posts: 31
CoW is on a distinguished road
Send a message via AIM to CoW
ok, no more compile errors (yay!)

but now after dateValid() runs the program crashes....
CoW is offline   Reply With Quote
Old 11-03-2003, 06:03 PM   #4 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
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.
joe_bruin is offline   Reply With Quote
Old 11-03-2003, 07:17 PM   #5 (permalink)
CoW
Registered User
 
CoW's Avatar
 
Join Date: May 2003
Location: Paris, France
Posts: 31
CoW is on a distinguished road
Send a message via AIM to CoW
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.
CoW is offline   Reply With Quote
Old 11-03-2003, 07:42 PM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 11-03-2003, 07:45 PM   #7 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 11-03-2003, 08:02 PM   #8 (permalink)
CoW
Registered User
 
CoW's Avatar
 
Join Date: May 2003
Location: Paris, France
Posts: 31
CoW is on a distinguished road
Send a message via AIM to CoW
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..
CoW is offline   Reply With Quote
Old 11-03-2003, 10:45 PM   #9 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
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.

Quote:
my cins are fine.
not quite. i'd advice you to look at the values of date[1] and date[2] and figure out why they are incorrect.
joe_bruin is offline   Reply With Quote
Old 11-04-2003, 05:11 AM   #10 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
thanks for checkin that joe .. maybe i should put permanent disclaimers in my sig
__________________
Mike
sde is offline   Reply With Quote
Old 11-04-2003, 05:42 AM   #11 (permalink)
CoW
Registered User
 
CoW's Avatar
 
Join Date: May 2003
Location: Paris, France
Posts: 31
CoW is on a distinguished road
Send a message via AIM to CoW
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]
CoW is offline   Reply With Quote
Old 11-04-2003, 06:29 AM   #12 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 11-04-2003, 10:09 AM   #13 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
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.
joe_bruin is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
combining arrays Engineer Standard C, C++ 11 03-12-2005 10:23 AM
Dynamic Arrays, the 'right' way. Mr.Anderson Standard C, C++ 10 10-05-2004 06:00 AM
Help :: Loops & Arrays Jarumajoo Standard C, C++ 3 12-01-2003 08:48 PM
sort arrays Apodysophilia Java 9 04-04-2003 06:18 AM


All times are GMT -8. The time now is 03:51 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting