14 8063 Ganon11 3,652
Recognized Expert Specialist
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
-
};
-
oler1s 671
Recognized Expert Contributor
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();
-
-
}
oler1s 671
Recognized Expert Contributor
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
oler1s 671
Recognized Expert Contributor
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...hopefull y 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
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 integers. My code
is listed below, but the problem is that my output of sum is wrong.
For example, I am using 1347830 for my integers and the program outputs
373 and after adding 373 you should get 13 for the total, but that
doesn't happen. Any help...
|
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 for 10 students.
The array is initialized with -1 in the beginning - this is because if
a student did not take a test yet I want to be able to sort and find
this out. There is a menu option to enter grades which calls a
function in order to enter...
|
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 "word", or "eelttr" for "letter".
Build an array of all the keys, and sort it using a bubble sort. I have to write a modified version of bubble sort that maintains also the array of the primary words.
For instance, if I had ,
our initial array of keys...
|
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 dimensional array of City #1 temps using a selection sort and display the sorted array
c. Extract the temps recorded from all Cities as 3rd entry and store in a one dimensional array, display the array
d. Sort the one dimensional array of temps...
|
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 integers. How do i get a bubble sort to put names in alphabetical order?
| |
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 and need help.
I need to ask a different question that is similar to this, make up something different because this is my homework question and that is not allowed.
Question:
The First program is required to ask for 5 students names and their...
|
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, another inputboxes will appear asking for the items to be sorted...
then the items you inputed will be printed in the 1st listbox..
then when you click the 2nd cmd button the items in the first listbox will appear in the second listbox but they...
|
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 this array. please help me.
my scenario:
assume that we have a big 2d char array for example students for 20 persons an 30 character for each person. first 15 chars contains first name and the rest is last name.
no i want to sort this array...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |