14 7965
Two things I immediately notice:
1) This is C++ (as I see from you using strings, cin, cout, etc.), so why are you using char* to hold names and such? That's what std::strings are for.
2) In your main(), you are trying to call methods on an object, but you haven't made a student object. You need to say: - student myStudent;
-
-
myStudent.displayMessage();
or something of the sort.
In addition, I'm not sure your design is entirely straightforward. A student shouldn't have to know how many classmates he/she has (i.e. the array size), nor should he/she need to have information about inputting/outputting information (getData(), displayMessage()).
An easier to design (not to mention easier to work with) design would be like this: the Student class has a first name and last name as well as an array/vector of grades, and a constructor which allows you to set these attributes. Then you either have another class, maybe Class, which will hold a collection of students and can sort them, etc.
Finally, if you are getting errors, it's usually easier to go ahead and post these errors as well, rather than letting repliers blindly try to analyze your code.
thanks for the help man, i got rid of all the errors and the program "runs' now. When it runs it just prints out getData() cout stuff 100 times, and then prints out Student Last name: folloed by a bunch of jibberish. The program requirments call for the program to promt the user for the number of students they would like to enter, that is the only reason i did it like that. I have commened out the bubble sort, becaussei can't figure out how to make it sort the last names. Any help with with getting the program working better would be apprecriated. here is what i have now, -
-
#include <iostream> // allows the program to output data to the screen
-
#include <string.h>
-
-
// students class definition
-
class students
-
{
-
public:
-
int getNumstudents(); // function to get the number of students user wants to enter
-
void getData(); // function to get first and last name and the students grade
-
void displayMessage(); // displays welcome message
-
//void sortData (); // function to alphabetize by last name using buble sort
-
void printData (); // function to print data
-
const static int numStudents = 100;
-
private:
-
char firstName[100]; // array of students first names
-
char lastName[100]; // array of students last names
-
int grades[100]; // array of students grades
-
};
-
Quick note: it's <string>, not <string.h> . You do know how standard headers work, do you not?
So, what have you done to figure out where the problem areas are? It's not nice to take your non working program, dump it on us, and say "figure out what's wrong please".
I'm sorry that is not what i have meant to do...I just wanted suggestions and i got some that helped alot.. like the idea of using strings, so now i am using string vectors and completly redid my program. I do appreicate your guys help and sorry if came off stupid...i was just really lost.. it being my first large program and all. Here is what i have now as sort of prototype using the vectors. I can get it to read the names i eneter, but for some reason as soon as i am done enetering them the program quits instead of displaying them. -
#include <iostream> // allows the program to output data to the screen
-
#include <string>
-
-
// students class definition
-
class students
-
{
-
public:
-
void getData(); // function to get first and last name and the students grade
-
void displayMessage(); // displays welcome message
-
-
private:
-
-
};
- #include <iostream> // allows the program to output data to the screen
-
#include <conio.h>
-
#include <iomanip>
-
#include <cstdlib>
-
#include <string>
-
#include <vector>
-
#include <algorithm>
-
#include <numeric>
-
#include "students.h" // gradebook class defintion
-
-
-
using std::cout; // program uses cout
-
using std::cin; // program uses cin
-
using std::endl; // program uses endl
-
using std::setprecision; // set numeric output precision
-
using std::fixed; // ensures that decimal point is displayed
-
using std::setw;
-
using std::string;
-
using std::vector;
-
-
-
void students::displayMessage()
-
{
-
-
cout << "Welcome to the Student Scores Application." << endl << endl;
-
}
-
-
-
void students::getData()
-
{
-
int numStudents;
-
vector<string> student_names(numStudents);
-
-
cout <<"Enter number of students to enter: ";
-
cin >> numStudents;
-
-
for (int i = 0; i <= numStudents; i++)
-
{
-
cout << "Enter student name (Enter to finish): ";
-
getline (cin, student_names[i]);
-
}
-
-
// sort them alphabetically
-
sort (student_names.begin(), student_names.end());
-
-
for (int i =0; i < numStudents; i++)
-
{
-
cout << student_names[i];
-
}
-
-
-
-
}
-
-
// function main begins program exectuion
-
int main()
-
{
-
students mystudent;
-
-
mystudent.displayMessage();
-
-
mystudent.getData();
-
-
}
I have two questions for you. Hopefully this gives you a hint.
int i = 0; i <= numStudents
You sure about that?
Next. Look at the following code: -
void createNumbers()
-
{
-
std::vector<int> numbers;
-
numbers.push_back(1);
-
numbers.push_back(3);
-
numbers.push_back(10);
-
}
-
-
void printNumbers()
-
{
-
createNumbers();
-
std::cout << numbers[0] << std::endl;
-
}
-
What's wrong with the above code?
I have two questions for you. Hopefully this gives you a hint.
You sure about that?
Next. Look at the following code: -
void createNumbers()
-
{
-
std::vector<int> numbers;
-
numbers.push_back(1);
-
numbers.push_back(3);
-
numbers.push_back(10);
-
}
-
-
void printNumbers()
-
{
-
createNumbers();
-
std::cout << numbers[0] << std::endl;
-
}
-
What's wrong with the above code?
the only problem i can see is that your just print the 0 space in the numbers array. So i am guessing that is what i am doing in my code..calling it to output nothing. and as far as being sure about i=0; i < numStudents; i++...i am pretty sure because that would call each array element 0 thorugh the size of array. right? i appreciate your method of help...i don't like being told the answer. thanks
You've missed both hints, so I'll throw it again in a slightly more indepth fashion. Let's start with the <= numStudents issue. I'll throw out sample code again. -
int numItems = 1;
-
std::vector<int> numbers(numItems);
-
numbers[0] = 50;
-
How many elements are there in numbers? Clearly 1. Let's loop over this vector. -
for (int i=0; i<=numItems; ++i) //remember that numItems=1
-
{
-
std::cout << numbers[i] << std::endl;
-
}
-
Write this out by hand. Write down i. Write down the evaluation of i<= numItems. If true, write down numbers[i]. Then repeat the loop. You'll see the problem (a very, very frequent one).
As to my second example. You also missed the issue.
the only problem i can see is that your just print the 0 space in the numbers array
Terminology first. It's a vector, not an array. There is no such thing as a 0 space. An index of 0 gets me the first element. Also, it's impossible to output, but have nothing as your output. You either output or you don't. If you do output, it must be something in memory. Now, you know that the computer works in 1s and 0s. That is, you either have a 1 or 0. "Nothing" is not a part of the binary system. But this all gets away from the real important point.
First, answer this question. -
#include <iostream>
-
-
int main()
-
{
-
std::cout << i;
-
}
-
What's wrong here?
Then: -
#include <iostream>
-
-
void someFunc()
-
{
-
int i=50;
-
}
-
-
int main()
-
{
-
std::cout << i;
-
}
-
What's wrong here?
Then: -
#include <iostream>
-
-
void someFunc()
-
{
-
int i=50;
-
}
-
-
int main()
-
{
-
someFunc();
-
std::cout << i;
-
}
-
So what's wrong here? Don't hesitate to use your compiler to get a hint.
You've missed both hints, so I'll throw it again in a slightly more indepth fashion. Let's start with the <= numStudents issue. I'll throw out sample code again. -
int numItems = 1;
-
std::vector<int> numbers(numItems);
-
numbers[0] = 50;
-
How many elements are there in numbers? Clearly 1. Let's loop over this vector. -
for (int i=0; i<=numItems; ++i) //remember that numItems=1
-
{
-
std::cout << numbers[i] << std::endl;
-
}
-
Write this out by hand. Write down i. Write down the evaluation of i<= numItems. If true, write down numbers[i]. Then repeat the loop. You'll see the problem (a very, very frequent one).
As to my second example. You also missed the issue.
Terminology first. It's a vector, not an array. There is no such thing as a 0 space. An index of 0 gets me the first element. Also, it's impossible to output, but have nothing as your output. You either output or you don't. If you do output, it must be something in memory. Now, you know that the computer works in 1s and 0s. That is, you either have a 1 or 0. "Nothing" is not a part of the binary system. But this all gets away from the real important point.
First, answer this question. -
#include <iostream>
-
-
int main()
-
{
-
std::cout << i;
-
}
-
What's wrong here?
Then: -
#include <iostream>
-
-
void someFunc()
-
{
-
int i=50;
-
}
-
-
int main()
-
{
-
std::cout << i;
-
}
-
What's wrong here?
Then: -
#include <iostream>
-
-
void someFunc()
-
{
-
int i=50;
-
}
-
-
int main()
-
{
-
someFunc();
-
std::cout << i;
-
}
-
So what's wrong here? Don't hesitate to use your compiler to get a hint.
Alright on the first one the for loop is only ran through once when i=0, after that i = 1 and and so is numItems. So i see why that is wrong, but in my code i have declared int numStudents and then let the user define the number of students. So in my for loop i shouldn't have that problem...at least that is what i think.
And for the second questions on the codes you wrote, on all three of them you haven't declared i. But in my code i have declared i in the for loop. Maybe i am missing the big picture here. i will coninue to try to figure it out. I appreciate the help...i am sure it will click soon enough,
Alright i think i just had a revalation. i realized what you were saying about i < numStudents. I need to set it up so i is less than or equal to the size of the vector. so this is how i changed my code... and it worked it printed the names in alphbetical order. - void students::getData()
-
{
-
int numStudents;
-
vector<string> student_names(numStudents);
-
-
cout <<"Enter number of students to enter: ";
-
cin >> numStudents;
-
-
for (int i = 0; i <= numStudents; i++)
-
{
-
cout << "Enter student name (Enter to finish): ";
-
getline (cin, student_names[i]);
-
}
-
-
// sort them alphabetically
-
sort (student_names.begin(), student_names.end());
-
-
for (int i =0; i <= student_names.size(); i++)
-
{
-
cout << student_names[i];
-
}
-
-
-
-
}
thanks for the help...hopefully i will be ok from here on out....I know it is printing out right, but did i do it correctly, i jst want to make sure i am learning properly. thanks very much
Ok, I guess I made things a bit more confusing... -
int numItems = 1;
-
std::vector<int> numbers(numItems);
-
numbers[0] = 50;
-
for (int i =0; i<= numItems; ++i)
-
{
-
std:: cout << numbers[i] << '\n';
-
}
-
Let's run through this. -
Output:
-
i i<=numItems numbers[i]
-
0 0<=1 true numbers[0]==50
-
1 1<=1 true numbers[1]==?
-
Do you see the problem?
And don't worry about the whole declaring i thing. I guess I pulled out the wrong idea with it. It's a design issue you have in your class, and you'll realize it soon enough.
Ok, I guess I made things a bit more confusing... -
int numItems = 1;
-
std::vector<int> numbers(numItems);
-
numbers[0] = 50;
-
for (int i =0; i<= numItems; ++i)
-
{
-
std:: cout << numbers[i] << '\n';
-
}
-
Let's run through this. -
Output:
-
i i<=numItems numbers[i]
-
0 0<=1 true numbers[0]==50
-
1 1<=1 true numbers[1]==?
-
Do you see the problem?
And don't worry about the whole declaring i thing. I guess I pulled out the wrong idea with it. It's a design issue you have in your class, and you'll realize it soon enough.
I understand the problem in the example you gave. Becasue numbers[0] = 50 that when the for loop tries to call numbers[1] there is nothing there, even though the condition is true....wait are you saying that when i am setting myy for loop to i <= numStudents, that when it gets to that last element there is nothing there because the vector starts at 0 and numstudents starts at 1. so i would set the for loop to i <= numStudents -1. that makes since...but then again maybe i am completly wrong again. I apologize if i am complety wrong again, i dont mean to waste you time, but i really appreciate it. I have been up for the past 24 hrs working on this program , i think i just need to sleep and it will make more sense in the morning. ill give it a try though beforei hit the sack.. thanks again.
I understand the problem in the example you gave. Becasue numbers[0] = 50 that when the for loop tries to call numbers[1] there is nothing there, even though the condition is true....wait are you saying that when i am setting myy for loop to i <= numStudents, that when it gets to that last element there is nothing there because the vector starts at 0 and numstudents starts at 1. so i would set the for loop to i <= numStudents -1. that makes since...but then again maybe i am completly wrong again. I apologize if i am complety wrong again, i dont mean to waste you time, but i really appreciate it. I have been up for the past 24 hrs working on this program , i think i just need to sleep and it will make more sense in the morning. ill give it a try though beforei hit the sack.. thanks again.
i <= numStudents -1
Will work and accomplish your goal, or you could write
i < numStudents
which will do the same thing.
Basically, because arrays and vectors are 0 indexed (that is, the first element is at index 0), the last index is always size - 1. So, in a vector of size 5, the elements are at index 0, 1, 2, 3, and 4. There is no guarantee that anything useful can be found in index 5.
hey man thanks that worked wonders. I have edited the code and it does run smoother and doesnt crash. The problem i run into now is that when i run it last name and first name are displayed at the same time for the user to enter. Like this last name: first name:. How get them to be seperate so the user can enter them individually. Also it not sorting them but i think it has to do with the program not getting the first and last name seperatly. - #include <iostream> // allows the program to output data to the screen
-
#include <conio.h>
-
#include <iomanip>
-
#include <cstdlib>
-
#include <string>
-
#include <vector>
-
#include <algorithm>
-
#include <numeric>
-
#include "students.h" // gradebook class defintion
-
-
-
using std::cout; // program uses cout
-
using std::cin; // program uses cin
-
using std::endl; // program uses endl
-
using std::setprecision; // set numeric output precision
-
using std::fixed; // ensures that decimal point is displayed
-
using std::setw;
-
using std::string;
-
using std::vector;
-
-
-
void students::displayMessage()
-
{
-
-
cout << endl << "Welcome to the Student Scores Application." << endl << endl;
-
}
-
-
-
void students::getData()
-
{
-
int numStudents = 0;
-
string name;
-
int score = 0;
-
int i = 0;
-
-
cout <<"Enter number of students to enter: ";
-
cin >> numStudents;
-
-
vector<string> student_lastnames(numStudents);
-
vector<string> student_firstnames(numStudents);
-
vector <int> student_score(numStudents);
-
-
do
-
-
{
-
cout << "Student " << i + 1 <<" last name: ";
-
getline (cin, name);
-
student_lastnames.push_back(name);
-
-
cout << "Student " << i + 1 <<" first name: ";
-
getline (cin, name);
-
student_firstnames.push_back(name);
-
-
cout << "Student " << i + 1 <<" score: ";
-
cin >> score;
-
student_score.push_back(score);
-
-
i++;
-
}
-
-
while ( i < numStudents);
-
-
-
// sort them alphabetically
-
sort (student_lastnames.begin(), student_lastnames.end());
-
-
for (int l =0; l < student_lastnames.size(); l++)
-
{
-
cout << student_lastnames[l] << " " << student_firstnames[l] << " " << student_score[l];
-
}
-
-
-
-
}
-
-
// function main begins program exectuion
-
int main()
-
{
-
students mystudent;
-
-
mystudent.displayMessage();
-
-
mystudent.getData();
-
-
}
now the user can enter all the info indivudally. And it the sort works... the problem i run into now is that for some reason when it prints it always priints two zeros before the text. and while it sorts the last names it prints the last name that is sorted withe first name and score of the first entry. ex. user inputs
studnt 1 last name: oaks
student 1 first name: john
student 1 grade: 100
studnt 2 last name: hill
student 2 first name: mike
student 2 grade: 75
program prints out:
0 0 hill john 100 oaks mike 75
i realize that since i am just sorting the lastname vector, that it is storing the sorted last name with the user input of the first name. not sure how to fix it and get rid of the zeros. thanks for the help so far. - #include <iostream> // allows the program to output data to the screen
-
#include <conio.h>
-
#include <iomanip>
-
#include <cstdlib>
-
#include <string>
-
#include <vector>
-
#include <algorithm>
-
#include <numeric>
-
#include <fstream>
-
#include <ios>
-
#include <istream>
-
#include <limits>
-
#include "students.h" // gradebook class defintion
-
-
-
using std::cout; // program uses cout
-
using std::cin; // program uses cin
-
using std::endl; // program uses endl
-
using std::setprecision; // set numeric output precision
-
using std::fixed; // ensures that decimal point is displayed
-
using std::setw;
-
using std::string;
-
using std::vector;
-
using std::max;
-
-
-
void students::displayMessage()
-
{
-
-
cout << endl << "Welcome to the Student Scores Application." << endl << endl;
-
}
-
-
-
void students::getData()
-
{
-
int numStudents = 0;
-
string name;
-
int score = 0;
-
int i = 0;
-
-
cout <<"Enter number of students to enter: ";
-
cin >> numStudents;
-
cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
-
-
vector<string> student_lastnames(numStudents);
-
vector<string> student_firstnames(numStudents);
-
vector <int> student_score(numStudents);
-
-
do
-
-
{
-
cout << "Student " << i + 1 <<" last name: ";
-
cin >> name;
-
cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
-
student_lastnames.push_back(name);
-
-
-
-
-
cout << "Student " << i + 1 <<" first name: ";
-
cin >> name;
-
cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
-
student_firstnames.push_back(name);
-
-
cout << "Student " << i + 1 <<" score: ";
-
cin >> score;
-
cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
-
student_score.push_back(score);
-
-
i++;
-
}
-
-
while ( i < numStudents);
-
-
-
// sort them alphabetically
-
sort (student_lastnames.begin(), student_lastnames.end());
-
-
for (int l =0; l < student_lastnames.size(); l++)
-
{
-
cout << student_lastnames[l] << " " << student_firstnames[l] << " " << student_score[l];
-
}
-
-
-
-
}
-
-
// function main begins program exectuion
-
int main()
-
{
-
students mystudent;
-
-
mystudent.displayMessage();
-
-
mystudent.getData();
-
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: hoover_richard |
last post by:
I am a newbie to C++ and I need help with a simple program I am trying
to write. My program is designed to print all of the odd integers
contained in an array and output the sum of the odd...
|
by: Tonytt |
last post by:
Hi,
I'm pretty new to c-programming and am taking a course in it. I'm
trying to write a menu program using functions and a two-dimensional
array. The program consists of entering 4 test grades...
|
by: Slowjam |
last post by:
How do I correct the program below to search all the anagrams in a given file.
First, for each word, build another word (its key) with all its letters sorted. For instance, build "dorw" for...
|
by: kuchma2000 |
last post by:
Hi.
I have a C program (see code below) which I need to urgrade. All I need to do is:
a. Extract the temps for the City #1 and store in a one dimensional array, display the array.
b. Sort the one...
|
by: guest |
last post by:
I am doing a program for Intro to Computer Programming where we take an array of strings and we must sort them alphabetically. we are supposed to use a bubble sort, but i know the code if meant for...
|
by: hockeyjock |
last post by:
I need to develop a grade book program.
This is my first programming class. I have learned variables, data types, math operations and precedence, I HAVE not learned methods, I have read about 5 book...
|
by: anelie |
last post by:
the problem is:
using 2 listboxes and 2 command buttons..
when you click the 1st cmd button an inputbox will appear asking the number of items to be sorted..then when you inputed a number,...
|
by: mahdiahmadirad |
last post by:
Hi dears!
I wrote a simple bubble sort algorithm. it works properly when we compare full arrays but i want to sort a 2d array according to a specific part of array. it has some problem to swapping...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |