Question:
C++ question, I'm such a noob!parallel arrays?
summertime
2010-03-15 12:50:20 UTC
The assignment asks to make a main menu with 4 options and then make functions for each option. this is my int main.


#include
#include
#include //this is so function exit can be used
using namespace std;

void newcourse();
void coursenum();
void print();

int main()
{
//Main menu
int option;
int x;
int y;
cout<<"Main menu:"< cout<<"1. Enter new course information"< cout<<"2. Search a course by course number"< cout<<"3. Print a list of courses sorted by the course number"< cout<<"4. Exit"< // input option
cout << "Please select an option (1-4): ";
cin >> option;
if(option>=1 and option<=4){
if (option == 1)
{cout<< "1. Enter new course information"< newcourse();}
else if (option == 2)
{cout<<"2. Search a course by course number"< coursenum();
return main();}
else if (option == 3)
{cout<<"3. Print a list of courses sorted by the course number"< //print(x,y);
return main();}
else
{exit(4);}}
else
{cout << "Error: input data out of range"< return main();}

return 0;
}
void newcourse()
{//num-number of courses, name-instructorlastname, cap-capacity

string num[5];
string name[5];
int cap[5];
int x=100;

for(int i=0; i < x; i++){
cout << "course number (0 to exit): ";
cin>> num[i];
if( num[i] == "0"){
main(); }
cout<< "Instructor: ";
cin>> name[i];
cout<<"Capacity: ";
cin>>cap[i];
cout<< endl;}

}

I have made the function for newcourse() and im working on making the one for coursenum(). My problem is that i don't know how im supposed to get the input information from newcourse() into coursenum() so that i can do the search. any suggestions?idea? please and thank you!
Three answers:
Frank
2010-03-15 14:18:03 UTC
First I've noticed that you keep calling the main function to do your looping, what that's doing is opening up a ton of instances of the main function without ever exiting any functions at all, causing huge issues. What you should do is use while loops and code them to keep looping while menu items are being pressed and to end the loop when the exit key is pressed. I've redone it to give you an example. Also you can pass the variables by reference to the newcourse function so that it will retain the information to pass to the next function. Also instead of looping inside the newcourse function it would be better to loop in the main function but call newcourse repeatedly as needed. I also changed that in my example.Because you can't pass by reference an array. Here's my example. feel free to email me with any questions about it.





bool newcourse(string &num,string &name,int &cap);

void coursenum();

void print();



int main(int argc, char* argv[])

{

//Main menu

int option;

int x;

int y;

string num[101];

string name[101];

string num2,name2;

int cap[101];

int cap2;

int courses = 0;



do

{

cout<<"Main menu:"<
cout<<"1. Enter new course information"<
cout<<"2. Search a course by course number"<
cout<<"3. Print a list of courses sorted by the course number"<
cout<<"4. Exit"<
// input option

cout << "Please select an option (1-4): ";

cin >> option;

if(option>=1 && option<=4){

if (option == 1)

{cout<< "1. Enter new course information"<
while (newcourse(num2,name2,cap2))

{



if (++courses > 100)

{

cout<< "You've entered the max number of 100 courses"<
break;

}

else

{



num[courses-1] = num2;

name[courses-1] = name2;

cap[courses-1] = cap2;

}



}}

else if (option == 2)

{cout<<"2. Search a course by course number"<
//coursenum();

}

else if (option == 3)

{cout<<"3. Print a list of courses sorted by the course number"<
//print(x,y);

}

}

else

{

cout << "Error: input data out of range"<
}

} while (option != 4);



return 0;

}





bool newcourse(string &num,string &name, int &cap)

{//num-number of courses, name-instructorlastname, cap-capacity



bool bReturn = true;;



cout << "course number (0 to exit): ";

cin>> num;

if( num == "0")

{

bReturn = false;

}

else

{

cout<< "Instructor: ";

cin>> name;

cout<<"Capacity: ";

cin>>cap;

cout<< endl;

}



return bReturn;

}
?
2017-01-17 08:58:02 UTC
you're able to do a right this moment reproduction by applying doing memset(array1, array2, sizeof(array1[0])*lengthl) ; you could alter length to experience your q length and you will upload to array1 (or 2) for offsets memset (array1+offset, array2, sizeof(array1[0])*length) ; strlen furnish you with the size of a char* array, there is no thank you to detect the size of a array of yet another form, even inspite of the undeniable fact which you will trick strlen into telling you in case you solid the array to a char* and be certain the final ingredient is often 0 ** unquestionably forget approximately the tricking strlen ingredient this is an extremely undesirable theory
Benny
2010-03-15 13:10:36 UTC
Use global variables.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...