![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 343
Rep Power: 4
![]() |
simple program problem
I have to write a program that prompts the user to enter a course by department course number semester taken and what the final grade was. Then the user has the option to print the courses entered or check the courses entered against the requirements. Right now i am working on entering the courses and printing the courses entered. I can enter and print courses fine the first time. However, if i try to enter 1 course and then say print that course out. Then i ask to add another course and print that it should print both courses however it only prints the second course entered. I'm very new to C++ i just have some java experience so any help would be very appreciated. Heres the code:
#include <iostream>
#include <string.h>
using namespace std;
struct classInfo
{
char department[2];
int courseNumber;
char semesterTaken[5];
char grade;
};
struct classInfo clInfo[100];
int loc = 0;
int i = 0;
void print()
{
while(i < loc)
{
cout << clInfo[i].department << " "
<< clInfo[i].courseNumber << " "
<< clInfo[i].semesterTaken << " "
<< clInfo[i].grade
<< endl;
i++;
}
cout << endl;
}
void addClass ()
{
cout << "Please enter the department code: ";
cin >> clInfo[loc].department;
cout << "Please enter the course number: ";
cin >> clInfo[loc].courseNumber;
cout << "Please enter the semester: ";
cin >> clInfo[loc].semesterTaken;
cout << "Please enter the grade earned: ";
cin >> clInfo[loc].grade;
cout << endl;
loc++;
}
int main()
{
int option;
while(option !=4)
{
cout << "Please make a selection: \n"
<< "1. Add a new class. \n"
<< "2. Print your current list of classes. \n"
<< "3. Check your courses against the requirements. \n"
<< "4. Quit \n"
<< "Your selection: ";
cin >> option;
cout << endl;
if(option == 1)
addClass();
if(option == 2)
print();
}
return 0;
} |
|
|
|
|
|
#2 | |
|
Professional Programmer
|
have you tried..
void print()
{
while(i < loc)
{
cout << clInfo[i].department << " "
<< clInfo[i].courseNumber << " "
<< clInfo[i].semesterTaken << " "
<< clInfo[i].grade
<< endl;
i++;
}
cout << endl;
}printnum;
int main()
{
int i = 0;
addClass();
if(addClass)
{
i += 1;
printnum[i];
}I'm really hungry so not really into it as much, i hope you understand what i'm trying to say.
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ |
|
|
|
|
|
|
#3 | |
|
Programmer
Join Date: Dec 2005
Posts: 53
Rep Power: 3
![]() |
Quote:
First off... void print()
{
...
}printnum;What is printnum? I know you can create instance members of classes like that, but never seen anyone do that with a function. Also... addClass(); if(addClass) First you use addClass like a function and then you use it like a variable. Which is it? I realize you're probably learning C++, but the best way to learn it is:
Anyhow, to the original poster. The reason your print function isn't printing out everything is because you must set "i = 0" in the print function. You have a global variable (which is generally a bad idea) that is incremented up to loc, but it's never set back to the beginning. There are other issues however. Department code can only be one character, because you've declared "department" to be 2 characters, but one of those characters is going to be occupied by \0 because you're using a char* string. You should probably stick to std::string if you're using C++, unless of course your professor does not allow it. |
|
|
|
|
|
|
#4 | ||
|
Professional Programmer
|
Quote:
PS. And no, i don't have a clue what i'm doing when i type code in... but i still get it right more than half the time so it's ok.
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ |
||
|
|
|
|
|
#5 | |
|
Programmer
Join Date: Dec 2005
Posts: 53
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
|
|
#6 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Jason, you were kinda harsh, but I agree when you say that code should have been checked first. There's nothing worse than teaching a newbie the wrong thing - I know it confuses the heck out of me when people do that.
|
|
|
|
|
|
#7 |
|
Programmer
Join Date: Dec 2005
Posts: 53
Rep Power: 3
![]() |
Here's some code of how I would do it. It's not perfect, but then again I haven't programmed C++ in years (mostly a C# guy). I think by placing the code for the functions in the class it will suggest to the compiler that it should be inlined, but the functions are so simple it shouldn't matter that much.
The output could probably be cleaner. Maybe use some \t or setw(...) to make some nice preformatted output. #include <iostream>
#include <string>
#include <vector>
class Transcript{
public:
void print()
{
for(unsigned int i = 0; i < m_Classes.size(); i++)
{
std::cout << m_Classes[i].m_Department << " "
<< m_Classes[i].m_CourseNumber << " "
<< m_Classes[i].m_SemesterTaken << " "
<< m_Classes[i].m_Grade << std::endl;
}
std::cout << std::endl;
}
void addClass()
{
ClassInfo temp;
std::cout << "Please enter the department code: ";
std::cin >> temp.m_Department;
std::cout << "Please enter the course number: ";
std::cin >> temp.m_CourseNumber;
std::cout << "Please enter the semester: ";
std::cin >> temp.m_SemesterTaken;
std::cout << "Please enter the grade earned: ";
std::cin >> temp.m_Grade;
std::cout << std::endl;
m_Classes.push_back(temp);
}
private:
struct ClassInfo
{
std::string m_Department;
int m_CourseNumber;
std::string m_SemesterTaken;
char m_Grade;
};
std::vector<ClassInfo> m_Classes;
};
int main()
{
Transcript myTranscript;
int option = -1;
while(option !=4)
{
std::cout << "Please make a selection: \n"
<< "1. Add a new class. \n"
<< "2. Print your current list of classes. \n"
<< "3. Check your courses against the requirements. \n"
<< "4. Quit \n"
<< "Your selection: ";
std::cin >> option;
std::cout << std::endl;
switch(option)
{
case 1:
myTranscript.addClass();
break;
case 2:
myTranscript.print();
break;
case 3:
// Add Functionality
break;
case 4:
std::cout << "Quitting..." << std::endl;
break;
default:
std::cout << "Error: Choose again." << std::endl << std::endl;
}
}
} |
|
|
|
|
|
#8 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 343
Rep Power: 4
![]() |
ok i apologize. I should have made this clear in the beginning but i can't use classes to make this program. I can only use structs and subroutines.
|
|
|
|
|
|
#9 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 850
Rep Power: 4
![]() |
Without rearranging your whole program as Jason did, the simple fix is that you have a global variable called i which is not being reset when you print. You don't need a globale variable here and I would certainly recommend against ever having a global variable called i.
Delete the global variable i and change your print loop to look something like: for (int i = 0; i < loc; i++)
{
cout << clInfo[i].department << " "
<< clInfo[i].courseNumber << " "
<< clInfo[i].semesterTaken << " "
<< clInfo[i].grade
<< endl;
i++;
}The int declaration inside the for loop declares it as local to the for (depending on your compiler, it might also be available outside the for loop, but it is still local to the function). |
|
|
|
|
|
#10 | ||
|
Programmer
Join Date: Dec 2005
Posts: 53
Rep Power: 3
![]() |
Quote:
Because... Quote:
|
||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|