Jan 16, 2017 at 11:37am UTC
Hello....
I'm trying to make a program to input Course details and display their details as per course semester and year which the user inputs.
my program:-
#include<iostream>
#include <string>
#include<cstring>
#include <conio.h>
#include <iomanip>
using namespace std;
struct Course;
struct Course{
string CourseCode[10];
string CourseName[40];
string Semester[10];
string Year[10];
};
void InputCourse(int k,char array1[50],char array2[50],Course s1[10]);
void DisplayHeaders();
void DisplayCourse(string Ccode[5],string Cname[5],string Csemester[5],string Cyear[5]);
int main()
{
int maxCourse=6;
char Courseinfo[50];
char SemesterStart[50];
Course Subj[6];
void InputCourse(int k,char array1[50],char array2[50],Course s1[10]);
DisplayHeaders();
void DisplayCourse(string Ccode[5],string Cname[5],string Csemester[5],string Cyear[5]);
cout<<endl<<endl;
system("pause");
}
void DisplayHeaders()
{
cout<<"\n#"<<setw(4)<<"Course Code"<<setw(20)<<"Course Name"<<setw(15)<<"Semester"<<setw(5)<<"Lounching Year"<<endl;
cout<<"\n--------------------------------------------------------------------------------------------\n"<<endl;
}
void InputCourse(int k,char array1[50],char array2[50],Course s1[10])
{
char *CourseCodeName;
char *SemesterYear;
for (int i=0; i<k; i++)
{
cout<<"Enter Course name along with course code: ";
cin.getline(array1,50,'\n');
CourseCodeName=strchr(array1,' ');
if(CourseCodeName)
{
char *CourseCodeName="\0";
char strcpy(s1[i].CourseCode, array1);
CourseCodeName++;
strcpy(s1[i].CourseName,array1);
}
cout<< "Enter Semester: ";
cin.getline(array2,50,'\n');
strcpy(s1[i].Semestre,array2);
SemesterYear=strchr(array2,' ');
if(SemesterYear)
{
*SemesterYear="\0";
SemesterYear++;
strcpy(s1[i].Year,array1);
}
}
cout<<endl<<endl<<endl;
}
void DisplayCourse(string Ccode[5],string Cname[5],string Csemester[5],string Cyear[5])
{
for(int i= 0;i<5; i++)
{
std::cout.width(1); std::cout << std::right << i+1<<" ";
std::cout.width(6); std::cout << std::right << Ccode[i]<<"\t";
std::cout.width(30); std::cout << std::right << Cname[i] << "\t";
std::cout.width(12); std::cout << std::right << Csemester[i] << "\t";
std::cout.width(4); std::cout << std::right << Cyear[i] << endl;
}
cout<<"\n\n";
}
Last edited on Jan 16, 2017 at 11:53am UTC
Jan 16, 2017 at 5:17pm UTC
It looks like you're trying to treat C++ strings as C strings. I really suggest you go through your program and get rid of all the C-strings and use the C++ string.
Look at this snippet:
1 2 3 4 5 6 7
struct Course
{
string CourseCode[10];
string CourseName[40];
string Semester[10];
string Year[10];
};
Why are you creating a bunch of arrays of C++ strings instead of just single instances of the C++ string?
1 2 3 4 5 6 7
struct Course
{
string CourseCode;
string CourseName;
string Semester;
string Year;
};
C++ strings don't require arrays to hold a single string and since you appear to be using an array of this structure all you would need is the single instances.
Next this snippet:
1 2 3 4 5
...
int main()
{
...
void InputCourse(int k, char array1[50], char array2[50], Course s1[10]);
Do you realize that you are not calling a function in the last line, that is a function prototype, not a function call. You would call the function by removing all the variable types and using instances of the correct type of variable. Why all the char arrays, you have a structure that has all of these variables why not just pass the structure instead?
Something more like:
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
struct Course
{
string CourseCode;
string CourseName;
string Semester;
string Year;
};
// Function prototypes
void InputCourse(Course course[10], int course_size);
int main()
{
const int maxCourse = 6;
Course Subj[maxCourse];
InputCourse(Course Subj, maxCourse);
...
// Function implementations.
void InputCourse(Course course[], int course_size)
{
for (int i = 0; i < course_size; i++)
{
cout << "Enter Course name along with course code: " ;
getline(cin, course[i].CourseName);
cout << "Enter Semester: " ;
getline(cin, course[i].Semester);
}
cout << endl << endl << endl;
}
Last edited on Jan 16, 2017 at 5:18pm UTC