14 8064 oler1s 671
Recognized Expert Contributor
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.
Ganon11 3,652
Recognized Expert Specialist
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 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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |