Hello there, I'm practicing structures and accessing data in a structures. I have here a program that gets an input of string and int from a user. I placed it inside a loop where every time the loop refreshes, it changes the information it gets. My problem is, how do i changed the input variable inside a loop?

here is my code, it is unfinished:

#include <iostream>
#include <pthread.h>
#include <string>
#define NUM_OF_STUDENTS 2
#define NUM_OF_INFO 4
using namespace std;

struct Student{
        string first_name, last_name, course;
        int id_no;

};

void print_student(void* f_name, void* l_name, void* course_, void* id_no_ );

Student student1;
Student student2;
string info[] = {"First Name: ", "Last Name: ", "Course: ", "ID #: "};

int main(){
       
        for(int j = 0; j<NUM_OF_INFO; j++){
                cout << info[j];
                cin >> student1.first_name;
        }


        return 0;
}

void print_student(void* f_name, void* l_name, void* course_, void* id_no_ ){

        for(int i = 0; i<NUM_OF_STUDENTS; i++){
                cout << "STUDENT #" << i+1 << endl;
                cout << "First Name: " << *((string*) f_name) << endl;
                cout << "Last Name: " << *((string*) l_name) << endl;
                cout << "Course: " << *((string*) course_) << endl;
                cout << "ID #: " << *((int*) id_no_) << endl;
                cout << "\n";
        }

}

Notice the for loop inside the main?

string info[] = {"First Name: ", "Last Name: ", "Course: ", "ID #: "};
for(int j = 0; j<NUM_OF_INFO; j++){
                cout << info[j];
                cin >> student1.first_name;
}

How do I change the input variable every time the loop refreshes? Like when the loop will refresh, the variable student1.first_name will change to student1.last_name coping with the cout << info[j] w/c is, when the loop increments to 1, will write "Last Name: ".

Thank you.

Recommended Answers

All 11 Replies

use an array. eg.

enum { MAX_STUDENTS = 16 };
Student students[MAX_STUDENTS] ;
for( int i=0 ; i<MAX_STUDENTS ; ++i )
{
  // ...
  cin >> students[i].first_name ;
  // etc.
}

you should also modify function print_student to take const string& args (not void*). passing by reference is as efficient as passing by pointer.

i am sorry, i did not read your question carefully and misunderstood it. to do what you want, we would need to use either pointers to members or templates. at this stage, you should probably just unroll the loop in your code.

Thanks! I'll try it. I have thought of using an array but I was too afraid to try because I have suspicions of producing an error, but I was wrong. I have come up with another solution, using temp variable of type string. I'll try it first. Thank you again man. You earn a rep point for your kindness.

Oh, I've already given you a rep point somewhere in my threads here, in this forum. I'm going to implement threading by this example. Uhmmm, maybe you could criticize my work after i finished, to see if I really did understand what I've read. If that's ok with you? hehehe...

...You earn a rep point for your kindness.

i think that most people would pay no attention to rep points. in any case, you should not be adding to the reputation of someone who misreads your question.

I was not able to view your reply before I posted the rep point thing. Oh well, that's fine with me, you have given me an idea. I could still use it. Maybe I'm just too kind to give rep points to those who are very helpful here, specially the mods.

This is my solution. I used a pointer of type Student(w/c is my structure). The problem is, I'm getting an error. There is something wrong with my code, *chuckles*, that I can't figure out.

here's the code:

#include <iostream>
#include <pthread.h>
#include <string>
#include <stdlib.h>
#define NUM_OF_STUDENTS 2
#define NUM_OF_INFO 4

using namespace std;

struct Student{
        string first_name, last_name, course;
        int id_no;

};

void print_student(void* f_name, void* l_name, void* course_, void* id_no_ );

Student student1;
Student student2;
Student* pStudent;
string info[] = {"First Name: ", "Last Name: ", "Course: ", "ID #: "};
string container[NUM_OF_INFO];
const char* temp;
int q,j;

int main(){
        for(j = 0; j<NUM_OF_INFO; j++){
                cout << info[j];
                cin >> container[j];
        }
        j = 0;
        pStudent->first_name = container[j];
        pStudent->last_name = container[j+1];
        pStudent->course = container[j+2];
        temp = container[j+3].c_str();
        q = atoi(temp);
        pStudent->id_no = q;

        print_student(pStudent->first_name, pStudent->last_name, pStudent->course, pStudent->id_no);
        return 0;
}

void print_student(void* f_name, void* l_name, void* course_, void* id_no_ ){

        for(int i = 0; i<NUM_OF_STUDENTS; i++){
                cout << "STUDENT #" << i+1 << endl;
                cout << "First Name: " << *((string*) f_name) << endl;
                cout << "Last Name: " << *((string*) l_name) << endl;
                cout << "Course: " << *((string*) course_) << endl;
                cout << "ID #: " << *((int*) id_no_) << endl;
                cout << "\n";
        }

}

and this is the error:

my_first_thread.cpp: In function `int main()':
my_first_thread.cpp:39: error: cannot convert `std::string' to `void*' for argument `1' to `void print_student(void*, void*, void*, void*)'

In addition,uhmm, how do I create a post of "code" that has line numbers in it... lol...for readability purposes...

I changed all the void to it's corresponding type. And I've added dynamic memory allocation to my pointer. Problem solved. But what if I did not changed the voids to it's corresponding data type? What do you think is the problem with the void thing?

haha, i forgot to place the reference operator & to the arguments passed. this solved my confusion. lol.

Member Avatar for iamthwee

I wouldn't have done it that way. You have over complicated it so. :D

hahaha, i was testing the void pointer, that's why im dying to get it right...hahaha...

are void pointers useless?? haha...

im just asking... why use void pointers as parameters anyway, if you can just use the type itself?

*chuckles*

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.