Help me Plz, :(

I'm very new for programming
i want to know that how i can make a function for receive the the name of students.i'm already stuck because
when i use that it's not save the
any input ...so plz help me:( here is my code...
if you have any best way for solve this plz teach me :) thanks!
and sorry for my bad english :)


student.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED
#include <string>

class student {

private:
std::string name[100];
};

#endif // STUDENT_H_INCLUDED 


student.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstring>
#include <string.h>
#include "student.h"

using namespace std;
 void student::setName(string inputName){
     
       
       for(int i=0;i<=name.length();i++){
       
         name[i] = inputName;


      }
       }




keeper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include "student.h"
#include <string>
using namespace std;


void setName(string);

int main(){

  int choice = 0; 
  std::string  inputName;

 while(choice!= 2){
   
    cout << "input choices"<<endl;
    cin >> choice;
 if(choice == 1){

       cout<<"input Name"<<endl;
       cin>>inputName;


       bool exist = false;
      for (int i=0; i<100; ++i) {
      if (name[i] != "" && name[i]== inputName) {
      exist = true;
      break;
      }
  }
     if (!exist) {
        bool added = false;
        for (int i=0; i<name.length(); ++i) {
        if (name[i] == "" && name[i] != inputName){

        
		 student st;
         st.setName(inputName);

         added = true;
         break;
        }
        }


        if (added) {
	cout<<"New Record ("<<inputName<<") Added sucessfully"<<endl;

        } else {
	cout<<"Error: data store is already full."<<endl;
	}
     }else {
     cout<<"A student record with the Name already exists"<<endl;
		    }
                }
        }

  }
Last edited on
i'm not sure exactly but I don't think you have declare 'Name' as a variable prior to using it here: -
if (Name[i] != "" && Name[i]== inputName)
You should really re-read about classes:

http://www.cplusplus.com/doc/tutorial/classes/

especially the "complete example of class CRectangle". Rather than a set_values method you'll need a GetName method.

That maybe a decent starting place.

edit: and create your student near the top of your while loop.
Last edited on
to: Lostsoulpary
sorry actually that is "name"
it's mistake of my typing when i post the question : )
It looks a bit strange to see a "student" class which has an array of 100 names.
It might be better to give the student a single name, and then have an array of students instead.
Topic archived. No new replies allowed.