no, UDFs are defined outside methods in C and C++.
In Java you could have created an inner class similar to this, in C++ that's not allowed.
Something like
#include <iostream>
#include <string>
struct student
{
std::string name; //name of student
int SSN; // social security number
std::string cardtype;
float balance;
};
int main()
{
student students[10];
for (int i = 0; i <= 10; i++ )
{
std::cout << "Enter student name\n";
std::cin >> students[i].name;
std::cout << "Enter social security number\n";
std::cin >> students[i].SSN;
std::cout << "Enter card type\n";
std::cin >> students[i].cardtype;
}
}
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
>in C++ that's not allowed
Are you sure? Maybe you should double check. It's allowed, though not very common because declaring a local structure restricts the scope of that structure to the function. That limits its usefulness.
>is this program i wrote so far going to store all the data entered by the user correctly?
No, it won't even compile. You're missing a closing brace for main and structure declarations must end with a semicolon. You also neglect to declare an instance of the structure. I imagine that something more like this is what you wanted:
#include <iostream>
int main()
{
struct students
{
char name[10]; //name of student
int SSN; // social security number
char cardtype[10];
float balance;
} array[10];
for ( int i = 0; i < 10; i++ )
{
std::cout << "Enter student name\n";
std::cin >> array[i].name[ i ];
std::cout << "Enter social security number\n";
std::cin >> array[i].SSN;
std::cout << "Enter card type\n";
std::cin >> array[i].cardtype[ i ];
}
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
hmm, so C does support local UDFs inside methods?
Never too old to learn I guess :)
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
>Naru, was this intentional?
No, occasionally I'll miss something. Even though the way it is is "technically" legal, it's highly unlikely to be what the OP intended. The lint part of my brain was working at the time, but not the logic checker. ;)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
i have a problem with this code, my cin statement is only reading in one character i think, how do i fix this?
Don't ask it to read only one character.
(And read some of the other responses above.)
(And please make an attempt at a fix!)
(And please learn to use code tags.)
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
can anyone help with this function?
I think you need to ask a better question.with this structure it says Given your definition, it should be possible to write the declaration:
CARD holder[10];
what does this exactly mean? im really confused.What are you talking about? Where didCARD enter the picture?
ok i have to right a function called AddHolder - that allows the user to insert a record into the database.
Where???
Copy and paste your exact code. If you have not even got the function name in your code, you're really asking someone to write it for you -- don't. Make an attempt.
Where is the attempt?
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
struct students
{
char name[10]; //name of student
int SSN; // social security number
char cardtype[10];
float balance;
} array[10];
for ( int i = 0; i < 10; i++ )
{
cout << "Enter student name\n";
cin >> array[i].name;
cout << "Enter social security number\n";
cin >> array[i].SSN;
cout << "Enter card type\n";
cin >> array[i].cardtype;
}
return 0;
}
For that matter, where isCARD?
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314