Just to refresh my concept I'm working on parallel arrays. One is used to store integer data and the other one for char data i.e GPA.
The problem compiles like a charm but the result is not correct, it displays the Student IDs correctly but not the GPA.
The simple cin works fine
I don't really know how to use cin.get and cin.getline using pointers.
In function enter I want to get the two-character-long string (plus one terminating null character).
Code listing:
#include <iostream>
#include <cstring>
using namespace std;
void enter(int *ar, char *arr, int size);
void exit(int *a, char *yo, int size);
int main()
{
const int id = 5;
const char grade = 5;
int *student = new int[id];
char *course = new char[grade];
cout << "\n";
enter(student, course, 5);
exit(student, course, 5);
}
void enter(int *ar, char *arr, int size)
{
for(int i = 0; i < size; i ++)
{
cout << "Student ID: " << i+1 << "\n";
cin >> *(ar+i);
cin.ignore();
cout << "Student Grade: " << i+1 << "\n";
cin.get(arr, 3);
}
}
void exit(int *a, char *yo, int size)
{
for(int i = 0; i < size; i ++)
{
cout << "ID And Grade Of Student #" << i+1 << ":";
cout << *(a+i) << "\t" << *(yo+j) << endl;
}
}
You are attempting to use part of the C++ language, but not embrace it entirely. There is no need for you to manage memory (at all) to solve this problem. Additionally, it would be much better to solve it using the standard language features:
struct Info
{
int StudentId;
std::string Grade; // this could easily be stored as an int or a char as well
};
int main()
{
const std::vector<Info>::size_type SIZE_LIMIT = 5;
std::vector<Info> vec(SIZE_LIMIT);
for (std::vector<Info>::size_type i = 0; i < SIZE_LIMIT; ++i)
{
std::cout << "Enter a Student ID: ";
std::cin >> vec[i].StudentId;
std::cout << "Enter a Grade: ";
std::cin >> vec[i].Grade;
}
std::for_each(vec.begin(), vec.end(), [&](const Info& i)
{
std::cout << "Student ID: " << i.StudentId << ", Grade: " << i.Grade << std::endl;
});
return 0;
}
Which can very easily be converted to account for more than 5 (e.g. virtually infinite) by adding an overload for std::istream& operator>>(std::istream&, Info&) and changing the for-loop to a std::copy operation.
If you absolutely want to keep your hands tied behind your back, you should at least make the following changes:
const unsigned int CLASS_SIZE = 5;
const unsigned int GRADE_SIZE = 5;
int student[CLASS_SIZE];
char course[CLASS_SIZE][GRADE_SIZE] = {};
// initialize course grades to empty strings, if you don't use the = {} above
for (unsigned int i = 0; i < CLASS_SIZE; ++i)
{
memset(course[i], 0, GRADE_SIZE);
}
// ...
// use your constants for your sizes
enter(student, course, CLASS_SIZE);
exit(student, course, CLASS_SIZE);
// ...
// NOTE: you should check to make sure the stream is in a good condition after each input - I leave the error checking code for you to implement
cout << "Student ID: ";
cin >> ar[i];
cout << "Student Grade: ";
cin >> arr[i]; // also note: since you are not using std::string, this can overflow! careful!
// ...
cout << "ID And Grade Of Student #" << i+1 << ":" << a[i] << "\t" << yo[i] << endl;
Related
In my class we recently got introduced to STL vectors. My professor has given us a program that uses arrays, and we are to convert it to use std::vectors instead. He would like us to use iterators, so we're not allowed to use square brackets, the push_back member function, or the at member function. Here's one of the for loops from the program I have to convert:
void readData(Highscore highScores[], int size)
{
for(int index = 0; index < size; index++)
{
cout << "Enter the name for score #" << (index + 1) << ": ";
cin.getline(highScores[index].name, MAX_NAME_SIZE, '\n');
cout << "Enter the score for score #" << (index + 1) << ": ";
cin >> highScores[index].score;
cin.ignore();
}
cout << endl;
}
`
I'm just not quite understanding how to convert them. so far, I was kind of able to get this: for (vector <Highscore> :: iterator num = scores.begin(); num < scores.end(); num++)for the for loop. It doesn't quite make sense to me so I was hoping I can get some more tips or even more information on how to convert them. I don't want an answer, simply just a tip. Thank you! (if its of any help, this is the program I am having to convert and these are the four headers we have to use
void getVectorSize(int& size);
void readData(vector<Highscore>& scores);
void sortData(vector<Highscore>& scores);
vector<Highscore>::iterator findLocationOfLargest(
const vector<Highscore>::iterator startingLocation,
const vector<Highscore>::iterator endingLocation);
void displayData(const vector<Highscore>& scores);
above are the headers that have to be used (having to use these instead of the programs headers)
#include <iostream>
using namespace std;
const int MAX_NAME_SIZE = 24;
struct Highscore{
char name[MAX_NAME_SIZE];
int score;
};
void getArraySize(int& size);
void readData(Highscore highScores[], int size);
void sortData(Highscore highScores[], int size);
int findIndexOfLargest(const Highscore highScores[], int startingIndex, int size);
void displayData(const Highscore highScores[], int size);
int main()
{
Highscore* highScores;
int size;
getArraySize(size);
highScores = new Highscore[size];
readData(highScores, size);
sortData(highScores, size);
displayData(highScores, size);
delete [] highScores;
}
void getArraySize(int& size){
cout << "How many scores will you enter?: ";
cin >> size;
cin.ignore();
}
void readData(Highscore highScores[], int size)
{
for(int index = 0; index < size; index++)
{
cout << "Enter the name for score #" << (index + 1) << ": ";
cin.getline(highScores[index].name, MAX_NAME_SIZE, '\n');
cout << "Enter the score for score #" << (index + 1) << ": ";
cin >> highScores[index].score;
cin.ignore();
}
cout << endl;
}
void sortData(Highscore highScores[], int numItems) {
for (int count = 0; count < numItems - 1; count++){
swap(highScores[findIndexOfLargest(highScores, count, numItems)],
highScores[count]);
}
}
int findIndexOfLargest(const Highscore highScores[], int startingIndex, int numItems){
int indexOfLargest = startingIndex;
for (int count = startingIndex + 1; count < numItems; count++){
if (highScores[count].score > highScores[indexOfLargest].score){
indexOfLargest = count;
}
}
return indexOfLargest;
}
void displayData(const Highscore highScores[], int size)
{
cout << "Top Scorers: " << endl;
for(int index = 0; index < size; index++)
{
cout << highScores[index].name << ": " << highScores[index].score << endl;
}
}
You maybe looking for one of two things.
If you want to add something to a vector, the function is push_back
vecScores.push_back(value) ; //in a for loop.
https://www.cplusplus.com/reference/vector/vector/push_back/
If you want to add something to a map, you could just use the form of
mapScore[index]=value ; // in a for loop.
https://www.cplusplus.com/reference/map/map/operator[]/
Probably your professor wants you to write something like this:
void readData(std::vector<Highscore>& highScores)
{
for (auto it = highScores.begin(); it != highScores.end(); ++it) {
cout << "Enter the name for score #" << std::distance(highScores.begin(), it) << ": ";
cin.getline(it->name, MAX_NAME_SIZE, '\n');
cout << "Enter the score for score #" << std::distance(highScores.begin(), it) << ": ";
cin >> it->score;
cin.ignore();
}
cout << endl;
}
where it is the iterator that's incremented via ++it from highScores.begin() to just before highScores.end(); then you access the members of the highScores's element "pointed by" it via it->member.
Here's a complete demo.
By the way, considering how much your professor likes void(some_type&) functions (and using namespace std;, if that was not your own idea), I would doubt you have much to learn from him. You better buy a good book.
I would do it like this, also get used to typing std::
Why is "using namespace std;" considered bad practice?
Also be careful with signed/unsigned, be precise about it.
If something can't have a negative value use unsigned types (or size_t)
#include <iostream>
#include <string>
#include <vector>
struct HighScore
{
std::string name;
unsigned int score;
};
// use size_t for sizes (value will always >0)
std::vector<HighScore> GetHighScores(size_t size)
{
std::vector<HighScore> highScores;
std::string points;
for (size_t index = 0; index < size; index++)
{
HighScore score;
std::cout << "Enter the name for score #" << (index + 1) << ": ";
std::cin >> score.name;
std::cout << "Enter the score for score #" << (index + 1) << ": ";
std::cin >> points;
// convert string to int
score.score = static_cast<unsigned int>(std::atoi(points.c_str()));
highScores.push_back(score);
}
std::cout << std::endl;
return highScores;
}
int main()
{
auto highScores = GetHighScores(3);
return 1;
}
I want to type this form in my program: s1[k[I]], but it doesn't let me. How can I fix this? Here's my code:
#include <iostream>
using namespace std;
int main(){
int N;
cin>>N;
string s1[N];
for(int k=0;k<N;k++)
{
cin >> s1[k];
}
int counter=0;
for(int k=0;k<N-1;k++)
{
for(int i=0;i<s1[k].size();k++)
{
if(s1[k[i]] == s1[k[i]])
{
cout << s1[k][i] << endl;
}
}
}
return 0;
}
Using the index ([]) operator is generally for arrays. But in this statement: if(s1[k[i]]==s1[k[i]]), k is an int, not an int[]. So how can you index an integer?
Additional: Don't use using namespace std; as it isn't a good practice.
Looks like you tried to join different ideas that don't realy want work together, mixing char arrays and std::strings can be done but is a no good idea because is messy.
As you can see in the example, use the std::string as intended and that's it.
#include <iostream>
int main(){
int N, i;
std::cout << "Add a number -> ";
std::cin >> N;
std::cout << "Add a string -> ";
std::string string;
std::cin >> string;
// recomended method
std::cout << "From std::string -> " << N << " | " << string << std::endl;
char * char_vec = new char [string.length()];
memcpy(char_vec, string.c_str(), sizeof(char) * string.length());
std::cout << "From array -> " << N << " | ";
for(i = 0; i < string.length(); i++){
std::cout << char_vec[i];
}
std::cout << std::endl;
delete [] char_vec;
return 0;
}
I have only a little experience in C++. I'm trying to write a program to print each element of the 'sales' array:
#include <iostream>
#include <iomanip>
using namespace std;
void printArray(int, int);
int main()
{
char chips[5][50] = {"mild", "medium", "sweet", "hot", "zesty"};
int sales[5] = {0};
int tempSales, counter;
const int i = 5;
for (counter = 0; counter < i; counter++)
{
cout << "Please enter in the sales for " << chips[counter] << ": ";
cin >> tempSales;
tempSales >> sales[counter][5];
}
cout << "{";
for (int counter = 0; counter < i; counter++)
{
cout << chips[counter] << ", ";
}
cout << "}" << endl;
cout << "{";
for (int counter = 0; counter < i; counter++)
{
cout << sales[counter] << ", ";
}
cout << "}" << endl;
return 0;
}
To solve this problem, I need to have the same commands and keywords I still have, and it can't be any advanced or weird syntax. For some reason, my input from the:
cin >> tempSales
Is not functioning. Here are the results:
{mild, medium, sweet, hot, zesty, }
{0,0,0,0,0, }
Whereas I just want to see 1, 2, 3, 4, and 5 for the second array. Why is it only print 0 and not reading my input? Please help!
Like rranjik stated, you shouldn't need a 2D array if you're only listing the number of sales, which it appears you're doing from what you provided, is this not the case?
Is it necessary for you to use the bitshift operator >> for your assignment? For a simple integer assignment it isn't really necessary, and you could do:
int sales[5] = {0}; Change the array to a simple array instead of 2D.
sales[counter] = tempSales; Use standard assignment for the integer on line 19
cout << sales[counter] << ", "; Change your output accordingly.
Hope this helps!
I don't think you need a 2D array for sales. Try cout << sales[counter][5] << ", "; or change int sales[5][6] = {0}; to int sales[5] = {0};. As Luke mentioned, use standard assignment sales[counter] = tempSales;.
I know this isnt the right kind of question to be asking, but for the life of me I could not figure out what is causing this problem.
I need to write a problem that takes a set number of integers or doubles and returns their sum.
I have written the code to make this work, making sure to check each time I changed something.
#include<iostream>
using namespace std;
template <class T>
class totalClass
{
private:
T *p;
T Total;
T sum;
int size;
public:
T total(int x)
{
size = x;
p = new T[x];
for (int i = 0; i < size; i++)
p[i] = T();
if (size > 1)
{
for (int i = 0; i < size; ++i)
{
cin >> sum;
Total += sum;
}
}
return Total;
}
};
int main()
{
int size, result1;
double result2;
cout << "Enter: ";
cin >> size;
cout << "the number of ints you wish to enter: Enter: " << size << " integers:";
totalClass<int> test;
result1 = test.total(size);
cout << " Total = " << result1 << endl;
cout << "Enter: ";
cin >> size;
cout << "the number of doubles you wish to enter: Enter: " << size << " doubles:";
totalClass<double> test2;
result2 = test2.total(size);
cout << " Total = " << result2 << endl;
}
My doubles are getting added up correctly but my integer addition always seems to add up to some crazy number. Is there something wrong with my problem that I cannot see?
If you forget to initialize a variable and attempt to use it or do math with it, you might end up with "crazy numbers." Make sure all of your variables are Initialized.
For this program a user must enter 10 contestants and the amount of second it took for them to complete a swimming race. My problem is that I must output the 1st, 2nd and 3rd placers, so I need to get the three smallest arrays (as they would be the quickest times) but I'm unsure on how to do it. Here is my code so far.
string names[10] = {};
int times[10] = { 0 };
int num[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int min1 = 0, min2 = 0, min3 = 0;
cout << "\n\n\tCrawl";
for (int i = 0; i < 10; i++)
{
cout << "\n\n\tPlease enter the name of contestant number " << num[i] << ": ";
cin >> names[i];
cout << "\n\tPlease enter the time it took for them to complete the Crawl style: ";
cin >> times[i];
while (!cin)
{
cout << "\n\tError! Please enter a valid time: ";
cin.clear();
cin.ignore();
cin >> times[i];
}
if (times[i] < times[min1])
min1 = i;
cout << "\n\n\t----------------------------------------------------------------------";
}
system("cls");
cout << "\n\n\tThe top three winners of the Crawl style race are as follows";
cout << "\n\n\t1st Place - " << names[min1];
cout << "\n\n\t2nd Place - " << names[min2];
cout << "\n\n\t3rd Place - " << names[min3];
}
_getch();
return 0;
}
As you can see, it is incomplete. I know how to get the smallest number, but its the second and third smallest that is giving me trouble.
your code is full of errors:
what do you do with min2 and min3 as long as you don't assign them?? they are always 0
try checking: cout << min2 << " " << min3;
also you don't initialize an array of strings like that.
why you use an array of integers for just printing number of input:
num? instead you can use i inside loop adding to it 1 each time
to solve your problem use a good way so consider using structs/clusses:
struct Athlete
{
std::string name;
int time;
};
int main()
{
Athlete theAthletes[10];
for(int i(0); i < 10; i++)
{
std::cout << "name: ";
std::getline(std::cin, theAthletes[i].name);
std::cin.sync(); // flushing the input buffer
std::cout << "time: ";
std::cin >> theAthletes[i].time;
std::cin.sync(); // flushing the input buffer
}
// sorting athletes by smaller time
for(i = 0; i < 10; i++)
for(int j(i + 1); j < 10; j++)
if(theAthletes[i].time > theAthletes[j].time)
{
Athlete tmp = theAthletes[i];
theAthletes[i] = theAthletes[j];
theAthletes[j] = tmp;
}
// printing the first three athletes
std::cout << "the first three athelets:\n\n";
std::cout << theAthletes[0].name << " : " << theAthletes[0].time << std::endl;
std::cout << theAthletes[1].name << " : " << theAthletes[1].time << std::endl;
std::cout << theAthletes[2].name << " : " << theAthletes[2].time << std::endl;
return 0;
}
I hope this will give u the expected output. But i suggest u to use some sorting alogirthms like bubble sort,quick sort etc.
#include <iostream>
#include<string>
using namespace std;
int main() {
int times[10] = { 0 };
int num[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int min1 = 0, min2 = 0, min3 = 0,m;
string names[10] ;
cout << "\n\n\tCrawl";
for (int i = 0; i < 10; i++)
{
cout << "\n\n\tPlease enter the name of contestant number " << num[i] << ": ";
cin >> names[i];
cout << names[i];
cout << "\n\tPlease enter the time it took for them to complete the Crawl style: ";
cin >> times[i];
cout<<times[i];
while (!cin)
{
cout << "\n\tError! Please enter a valid time: ";
cin.clear();
cin.ignore();
cin >> times[i];
}
if(times[i]==times[min1]){
if(times[min1]==times[min2]){
min3=i;
}else{min2 =i;}
}else if(times[i]==times[min2]){
min3=i;
}
if (times[i] < times[min1]){
min1 = i;
cout <<i;
}
int j=0;
while(j<i){
if((times[j]>times[min1])&&(times[j]<times[min2])){
min2 =j;
j++;
}
j++;
}
m=0;
while(m<i){
if((times[m]>times[min2])&&(times[m]<times[min3])){
min3 =m;
m++;
}
m++;
}
cout << "\n\n\t----------------------------------------------------------------------";
}
cout << "\n\n\tThe top three winners of the Crawl style race are as follows";
cout << "\n\n\t1st Place - " << names[min1];
cout << "\n\n\t2nd Place - " << names[min2];
cout << "\n\n\t3rd Place - " << names[min3];
return 0;
}
There is actually an algorithm in the standard library that does exactly what you need: std::partial_sort. Like others have pointed out before, to use it you need to put all the participant data into a single struct, though.
So start by defining a struct that contains all relevant data. Since it seems to me that you only use the number of the contestants in order to be able to later find the name to the swimmer with the fastest time, I'd get rid of it. Of course you could also add it back in if you like.
struct Swimmer {
int time;
std::string name;
};
Since you know that there always will be exactly 10 participants in a race, you can also go ahead and replace the C-style array by a std::array.
The code to read in the users then could look like this:
std::array<Swimmer, 10> participants;
for (auto& participant : participants) {
std::cout << "\n\n\tPlease enter the name of the next contestant: ";
std::cin >> participant.name;
std::cout << "\n\tPlease enter the time it took for them to complete the Crawl style: ";
while(true) {
if (std::cin >> participant.time) {
break;
}
std::cout << "\n\tError! Please enter a valid time: ";
std::cin.clear();
std::cin.ignore();
}
std::cout << "\n\n\t----------------------------------------------------------------------";
}
Partial sorting is now essentially a one-liner:
std::partial_sort(std::begin(participants),
std::begin(participants) + 3,
std::end(participants),
[] (auto const& p1, auto const& p2) { return p1.time < p2.time; });
Finally you can simply output the names of the first three participants in the array:
std::cout << "\n\n\tThe top three winners of the Crawl style race are as follows";
std::cout << "\n\n\t1st Place - " << participants[0].name;
std::cout << "\n\n\t2nd Place - " << participants[1].name;
std::cout << "\n\n\t3rd Place - " << participants[2].name << std::endl;
The full working code can be found on coliru.
This is not a full solution to your problem, but just meant to point you into the right direction...
#include <iostream>
#include <limits>
#include <algorithm>
using namespace std;
template <int N>
struct RememberNsmallest {
int a[N];
RememberNsmallest() { std::fill_n(a,N,std::numeric_limits<int>::max()); }
void operator()(int x){
int smallerThan = -1;
for (int i=0;i<N;i++){
if (x < a[i]) { smallerThan = i; break;}
}
if (smallerThan == -1) return;
for (int i=N-1;i>smallerThan;i--){ a[i] = a[i-1]; }
a[smallerThan] = x;
}
};
int main() {
int a[] = { 3, 5, 123, 0 ,-123, 1000};
RememberNsmallest<3> rns;
rns = std::for_each(a,a+6,rns);
std::cout << rns.a[0] << " " << rns.a[1] << " " << rns.a[2] << std::endl;
// your code goes here
return 0;
}
This will print
-123 0 3
As you need to know also the names for the best times, you should use a
struct TimeAndName {
int time;
std::string name;
}
And change the above functor to take a TimeAndName instead of the int and make it also remember the names... or come up with a different solution ;), but in any case you should use a struct similar to TimeAndName.
As your array is rather small, you could even consider to use a std::vector<TimeAndName> and sort it via std::sort by using your custom TimeAndName::operator<.