GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Nov-2008, 12:24
bostero22 bostero22 is offline
New Member
 
Join Date: Oct 2008
Posts: 21
bostero22 is an unknown quantity at this point

Help approaching a program


Hello everybody, I need help on how to start this program, I need to ask the user for this information and write it to a file: Student number, Student Name, ClassID, Credits and grade. The user is should be allowed to enter as many studentsas he likes (until '*' is entered) and he can also enter more than one class for each student. So, I was thinking a way to do this.. making a dimensional array, and store the each student in the rows, and the rest info on the colums... would that make any sense? is that posible? I need then to re-dispay the info that was entered. Any feedback on how should i start this program?

Any help would be much appreciated! thanks!
  #2  
Old 21-Nov-2008, 19:33
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 501
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Help approaching a program


Quote:
Originally Posted by bostero22
So, I was thinking a way to do this.. making a dimensional array...
This is one alternative, however, given the following:
Quote:
The user is should be allowed to enter as many studentsas he likes...
Storing all information in an array will not work because the array will have a finite size, & the user can arbitrarily input more data than the array can hold.

The simplest solution is get one record from the user & immediately write it to the file. Repeat until the sentinel value has been read.
  #3  
Old 22-Nov-2008, 12:38
zalezog zalezog is offline
New Member
 
Join Date: Oct 2007
Posts: 18
zalezog will become famous soon enough

Re: Help approaching a program


Quote:

Student number, Student Name, ClassID, Credits and grade




All seem to be logically related data items.
Just as ocicat poined it out
Quote:
Storing all information in an array will not work because the array will have a finite size, & the user can arbitrarily input more data than the array can hold.

making use of operators new and delete might be the key here.
Instead of storing them in an array,Suppose you create a strucure called student
CPP / C++ / C Code:
struct student
{
string number;
string Name;
.
.
.
};



Instead of creating an multidimensional array,an array of structures will make it much more simpler as ...

Quote:
I need then to re-dispay the info that was entered
For more
http://www.cplusplus.com/doc/tutorial/structures.html
This should give you a start,though the program doesn't respond to the star condition
CPP / C++ / C Code:
#include<iostream>
#include<stdlib.h>

using namespace std;
struct student
{
string name;
string number;       
};
int main()

{

student *iptr; //pointer for new operator,creating an array of structs   
iptr=0;

int i=0;
iptr=new student[2];//for two students only
do


{

cout<<"\nEnter id number ";
getline(cin,iptr[i].number);
cout<<endl<<"Enter name  " ;
getline(cin,iptr[i].name);
++i;
}

while(i<2);
cout<<endl<<"NAME"<<"\t"<<"ID\n";
for(int j=0;j<i;++j)
{
cout<<iptr[j].name<<"\t"<<iptr[j].number;
cout<<endl;
//write to files,if you want to.
}


delete iptr;
return 0;
}
Hope this gives you a start.
  #4  
Old 25-Nov-2008, 17:22
bostero22 bostero22 is offline
New Member
 
Join Date: Oct 2008
Posts: 21
bostero22 is an unknown quantity at this point

Re: Help approaching a program


Ok thanks I already started the program.. but i got stuck in another part...

I have to validate the studentNumber to be only 9 either alpha/digits inputs.
I was thinking of doing something like this but it is not working... is there any other way? or am I doing something wrong?

CPP / C++ / C Code:

cout << "Please enter the Student Number: \n";
     getline(cin, studentNumber);
     if (isalpha(studentNumber) || isdigit(studentNumber)) //Validate studentNumber to be alpha/digits format only.
     
     writeFile(studentNumber);

  #5  
Old 25-Nov-2008, 17:25
bostero22 bostero22 is offline
New Member
 
Join Date: Oct 2008
Posts: 21
bostero22 is an unknown quantity at this point

Re: Help approaching a program


oh and just in case.. studentNumber is a string .. can i use this types of validations in a string? or should I use another type of validation?
  #6  
Old 25-Nov-2008, 17:45
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 501
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Help approaching a program


Quote:
Originally Posted by bostero22
I have to validate the studentNumber to be only 9 either alpha/digits inputs.
Take a look at isalnum().
  #7  
Old 25-Nov-2008, 17:57
bostero22 bostero22 is offline
New Member
 
Join Date: Oct 2008
Posts: 21
bostero22 is an unknown quantity at this point

Re: Help approaching a program


will isalnum work for a string Class?
  #8  
Old 25-Nov-2008, 18:04
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 501
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Help approaching a program


Quote:
Originally Posted by bostero22
will isalnum work for a string Class?
Quickly glancing through the various member functions available:

http://www.cplusplus.com/reference/string/string/

...I don't see that this topic is expanded for strings in the aggregate, however, you can use isalnum() on each individual character within the string. See the c_str() member function for how to access the underlying C-style string implementation.
  #9  
Old 25-Nov-2008, 22:17
bostero22 bostero22 is offline
New Member
 
Join Date: Oct 2008
Posts: 21
bostero22 is an unknown quantity at this point

Re: Help approaching a program


Ok, I could not do it using strings. So now I am trying with chars, maybe thats easier, I got working the validation for the studentNumber to check if the it is alpha's or digits, but i also need to validate that what the user is entering is only 9 digits/alpha longs. I tried several ways, but I can't figure it out....

CPP / C++ / C Code:
void input() //Function definition for input.
{
     const int SIZE = 10;
     char studentNumber[SIZE]; //Set strings to hold data entered by the user.
     
     cout << "Please enter the Student Number: \n";
     cin.getline(studentNumber, SIZE);
     if(validateNumber(studentNumber, SIZE))
     {
     cout << studentNumber << endl;
     }
     else
     cout << "NOOOO\n";
     
     
}

bool validateNumber (char number[], int size)
     {
                    for(int count = 0; count < 10; count++)
                    {
                            if (isalnum(number[count]))
                            {
                            return true;
                            }
                    }
                            return false;
     }   
                            

Any tips??
  #10  
Old 26-Nov-2008, 02:10
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 501
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Help approaching a program


Quote:
Originally Posted by bostero22
Any tips??
Several.
Quote:
...i also need to validate that what the user is entering is only 9 digits/alpha longs.
You are making the assumption that users follow the rules. They don't. Practically, your buffer needs to be large, obscenely large. Otherwise if users type in twelve characters, you will have the situation of a buffer overflow which can potentially corrupt the stack & possibly crash execution. Code defensively.

By the way, entering twelve characters consumes thirteen characters in a buffer.
Quote:
...i also need to validate that what the user is entering is only 9 digits/alpha longs.
Fine. You can check the size of the string entered afterwards (strlen()) to ensure that all of your requirements are met, & scold them if they don't play by the rules. But I will caution you. If this is an assignment, make sure you are giving your instructor what he/she wants. Don't get caught up in frilly bells & whistles in which all newcomers are enamoured.

As for the code posted:
  • If constants are labeled & used, do so consistently.
  • Function validateNumber() returns on the first character which is correctly found to be an alphabetic or numeric character. Rather, what you want to be do is check all characters entered & return false if any character is not alphabetic or numeric. You may find that using a Boolean flag to track the state of all previously checked characters is useful.
  • Likewise, given that the buffer should be insanely large, there isn't any reason to check all characters in the buffer. Use strlen() to determine how many characters were entered, & check just them.
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Greatest and smallest integers bostero22 C++ Forum 8 01-Nov-2008 00:13
Equation solver RazoR C Programming Language 3 18-May-2008 10:24
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 11:13
Help with a complex program lordfuoco C++ Forum 5 24-Jun-2006 07:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:49.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.