I just started learning about arrays and I have a tenuous grasp on them.
Tried making this program in a lab today and keep getting an error that numJarsSold, typesOfSalsa, and totalJarsSold are undeclared identifiers in MyFunctions.cpp. I've found online already that people have had the same project and seen their code and I've written my own to run just in main but somehow running it separated I've managed to break it. Any help would be appreciated. Thanks.
Main.cpp
#include "MyFunctions.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
const int SIZE = 5;
string typesOfSalsa[SIZE] = { "Mild", "Medium", "Sweet", "Hot", "Zesty" };
int numJarsSold[SIZE]; // Holds Number of Jars of Salsa sold for each type
int totalJarsSold = getJarSalesData(typesOfSalsa, numJarsSold);
displayReport(typesOfSalsa, numJarsSold, totalJarsSold);
system("pause");
return 0;
}
MyFunctions.cpp
#include "MyFunctions.h"
using namespace std;
int getJarSalesData(string typesOfSalsa[], int numJarsSold[])
{
int totalJarsSold = 0;
for (int type = 0; type < SIZE; type++)
{
cout << "Jars sold last month of " << typesOfSalsa[type] << ": ";
cin >> numJarsSold[type];
while (numJarsSold[type] < 0)
{
cout << "Jars sold must be 0 or more. Please re-enter: ";
cin >> numJarsSold[type];
}
// Adds the number of jars sold to the total
totalJarsSold += numJarsSold[type];
}
return totalJarsSold;
}
int posOfLargest(int array[])
{
int indexOfLargest = 0;
for (int pos = 1; pos < SIZE; pos++)
{
if (array[pos] > array[indexOfLargest])
indexOfLargest = pos;
}
return indexOfLargest;
}
int posOfSmallest(int array[])
{
int indexOfSmallest = 0;
for (int pos = 1; pos < SIZE; pos++)
{
if (array[pos] < array[indexOfSmallest])
indexOfSmallest = pos;
}
return indexOfSmallest;
}
void displayReport(string[], int[], int)
{
int hiSalesProduct = posOfLargest(numJarsSold);
int loSalesProduct = posOfSmallest(numJarsSold);
cout << endl << endl;
cout << " Salsa Sales Report \n\n";
cout << "Name Jars Sold \n";
cout << "____________________________\n";
cout << typesOfSalsa[0] << " " << numJarsSold[0] << "\n";
cout << typesOfSalsa[1] << " " << numJarsSold[1] << "\n";
cout << typesOfSalsa[2] << " " << numJarsSold[2] << "\n";
cout << typesOfSalsa[3] << " " << numJarsSold[3] << "\n";
cout << typesOfSalsa[4] << " " << numJarsSold[4] << "\n";
for (int type = 0; type < SIZE; type++)
{
cout << left << setw(25) << typesOfSalsa[type] << setw(10) << numJarsSold[type] << endl;
cout << "\nTotal Sales: " << totalJarsSold << endl;
cout << "High Seller: " << typesOfSalsa[hiSalesProduct] << endl;
cout << "Low Seller: " << typesOfSalsa[loSalesProduct] << endl;
}
}
MyFunctions.h
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int getJarSalesData(string[], int[]);
int posOfLargest(int[]);
int posOfSmallest(int[]);
void displayReport(string[], int[], int);
In MyFunctions.cpp file the function := void displayReport(string[], int[], int) you didn't declared any variable name
it should be like
void displayReport(string typesOfSalsa[], int numJarsSold[], int totalJarsSold)
{
// copy the code
}
Related
I am trying to create a program where the user enters 5 candidates for an election along with their number of votes. The program's output creates a table where it shows each candidates name, their votes, percentage of votes, and the winner of the election. My code is below and a sample of the output is in a picture that I have uploaded.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <conio.h>
using namespace std;
const int numofcandidates = 5;
// declare variables and arrays
string candidateName[numofcandidates] = { "" };
int candidatevotes[numofcandidates];
float totalvotepercentage;
// declare methods
void takeNameandVote();
void printTable();
int main()
{
takeNameandVote();
printTable();
}
void takeNameandVote()
{
for (int i = 0; i < numofcandidates; i++)
{
cout << "Enter last name for candidate " << i + 1 << ": ";
cin >> candidateName[i];
cout << "Enter number of votes for this candidate: ";
cin >> candidatevotes[i];
}
}
void printTable()
{
// to set up % of total votes
int totalvotes = 0;
for (int i = 0; i < numofcandidates; i++)
{
totalvotes += candidatevotes[i];
}
cout << "Candidate\tVotes Received\t\t" << '%' << " of Total Votes" << endl;
for (int i = 0; i < numofcandidates; i++)
{
cout << candidateName[i] << '\t' << setw(18) << candidatevotes[i] << "\t" << fixed << showpoint << setprecision(2) << setw(16) << (candidatevotes[i] / (double)totalvotes * 100) << endl;
}
cout << "Total\t\t" << setw(10) << totalvotes << endl;
cout << endl;
}
Remove all \ts and only use std::setw:
std::cout << std::left << std::setw(18) << candidateName[i]
<< std::right << std::setw(8) << candidatevotes[i] << ...
So I've started learning vectors in c++, and I made this program, but I dont like the fact that I read the object data into a C style array, and then I transfer all the data into a vector.
Is it possible to read the data into the vector, without using the C style array?
This is my code:
#include <iostream>
#include <vector>
#include <stdlib.h>
using std::cout;
using std::cin;
using std::endl;
class number
{
private:
std::string name;
float n1_;
int n2_;
public:
number() = default;
~number() = default;
public:
void read();
float perMonth(){return n1_ * n2_;}
float perYear(){return perMonth() * 12;}
std::string get_Name(){return name;}
};
void number::read()
{
cout << "Worker name: "; cin >> name;
n1_ = rand() % 100 + 3;
n2_ = rand() % 100 + 3;
}
int main()
{
int n;
cout << "Insert number of workers:"; cin >> n;
number pers[30];
for(int i = 0; i < n; i++)
pers[i].read();
std::vector<number> people;
for(int i = 0; i < n; i++)
people.push_back(pers[i]);
for(number pr : people)
{
cout << "Name: " << pr.get_Name() << endl;
cout << "Salary per month: " << pr.perMonth() << endl;
cout << "Salary per year: " << pr.perYear() << endl << endl;
}
}
#include <iostream>
#include <vector>
#include <stdlib.h>
using std::cout;
using std::cin;
using std::endl;
class number
{
private:
std::string name;
float n1_;
int n2_;
public:
number() = default;
~number() = default;
public:
void read();
float perMonth(){return n1_ * n2_;}
float perYear(){return perMonth() * 12;}
std::string get_Name(){return name;}
};
void number::read()
{
cout << "Worker name: "; cin >> name;
n1_ = rand() % 100 + 3;
n2_ = rand() % 100 + 3;
}
int main()
{
int n;
cout << "Insert number of workers:"; cin >> n;
std::vector<number> people(n);
//N.B that pr is a non const reference in this loop
for(number& pr : people){
pr.read();
}
for(number pr : people)
{
cout << "Name: " << pr.get_Name() << endl;
cout << "Salary per month: " << pr.perMonth() << endl;
cout << "Salary per year: " << pr.perYear() << endl << endl;
}
}
Yes, You can put objects into std::vector without using an array.
int main()
{
int n;
cout << "Introduceti numarul de lucratori:"; cin >> n;
std::vector<number> people;
for(int i = 0; i < n; i++)
{
number pers();
pers.read();
people.push_back(per);
}
for(number pr : people)
{
cout << "Name: " << pr.get_Name() << endl;
cout << "Salary per month: " << pr.perMonth() << endl;
cout << "Salary per year: " << pr.perYear() << endl << endl;
}
}
In CPP file #1, I'm trying to loop through the array to see if any of the inputed names match Mordor or the_Vale (from CPP file #2 towards the bottom), however my loop is not working and I only know how to loop through a string array, not a char
#Header File#
#ifndef KINGDOM_H
#define KINGDOM_H
namespace westeros
{
class Kingdom
{
public: //Makes this class public to the rest of the code
char m_name[32];
int m_population;
int count = 0;
};
void display(Kingdom&);
void display(Kingdom* k, int x);
void display(Kingdom* k, int x, int z);
void display(Kingdom* k, int x, char foo[]);
}
#endif
#CPP FIle #1#
#include <iostream>
#include "kingdom.h"
void display(Kingdom* k, int x, char foo[])
{
int a = 0;
int found = 0;
cout << "Searching for kingdom " << foo << " in Westeros" << endl;
for (a; a < x; a++)
{
if (k[a].m_name == foo)
//(strcmp(k[a].m_name) == 0)//Not working
{
cout << k[a].m_name << ", population " << k[a].m_population << endl;
found = 1;
}
}
if (found == 0)
{
cout << foo << " is not part of Westeros." << endl;
}
}
}
## CPP File (main) #2##
#include <iostream>
#include "kingdom.h"
using namespace std;
using namespace westeros;
int main(void)
{
int count = 0; // the number of kingdoms in the array
// TODO: declare the kingdoms pointer here (don't forget to initialize it)
Kingdom* pKingdoms = nullptr;
cout << "==========" << endl
<< "Input data" << endl
<< "==========" << endl
<< "Enter the number of kingdoms: ";
cin >> count;
cin.ignore();
pKingdoms = new Kingdom[count];
for (int i = 0; i < count; ++i)
{
// TODO: add code to accept user input for the kingdoms array
int x = 0;
x++;
cout << "Enter the name for kingdom #" << x + i << ": ";
cin >> pKingdoms[i].m_name;
cout << "Enter the number people living in " << pKingdoms[i].m_name << ": ";
cin >> pKingdoms[i].m_population;
}
cout << "==========" << endl << endl;
// testing that "display(...)" works
cout << "------------------------------" << endl
<< "The first kingdom of Westeros" << endl
<< "------------------------------" << endl;
display(pKingdoms[0]);
cout << "------------------------------" << endl << endl;
// This is where I am having the problem
display(pKingdoms, count, "Mordor");
cout << endl;
display(pKingdoms, count, "The_Vale");
cout << endl;
cout << endl;
delete[] pKingdoms;
pKingdoms = nullptr;
return 0;
}
if (k[a].m_name == foo)
This is not how you compare two C-Style strings. This only compares the pointers, which should result in false almost certainly. You could use strcmp (#include <string.h>):
if (!strcmp(k[a].m_name, foo))
A better way, though, since you're programming in C++, use std::string:
std::string m_name;
and the comparison would have worked flawlessly.
I get an error saying: use of undeclared identifier 'again'.
I am trying to go from int main to void again and back after getting an answer.
Please explain to me why it won't work also. Thanks.
Here is my full code:
#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <string>
using namespace std;
{
string answer;
cout << "Would you like to enter another set of data? Y or N?" << endl;
cin << answer;
string yes = "Yes";
string no = "No";
if(a == yes)
{
main();
}
}
int main()
{
cout << "Kaitlin Stevers" << endl;
cout << "Exercise 11 - Vectors" << endl;
cout << "November 12, 2016" <<endl;
cout << endl;
cout << endl;
int size;
cout << " How many numbers would you like the vector to hold? " << endl;
cin >> size;
vector<int> numbers;
int bnumbers;
for (int count = 0; count < size; count++)
{
cout << "Enter a number: " << endl;
cin >> bnumbers;
numbers.push_back(bnumbers); // Adds an element to numbers
}
//display the numbers stored in order
cout << "The numbers in order are: " << endl;
for(int bcount = 0; bcount < size; bcount++)
{
cout << numbers[bcount] << " ";
}
cout << endl;
//display the numbers stored reversed
cout << "Here are the numbers in reverse order: " << endl;
reverse(numbers.begin(), numbers.end());
for(int rcount = 0; rcount < size; rcount++)
{
cout << numbers[rcount] << " ";
}
cout << endl;
again();
return 0;
}
void again()
}
You need to declare your fonction "again" before calling it :
#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <string>
using namespace std;
void again();
int main()
{
cout << "Kaitlin Stevers" << endl;
cout << "Exercise 11 - Vectors" << endl;
cout << "November 12, 2016" <<endl;
cout << endl;
cout << endl;
int size;
cout << " How many numbers would you like the vector to hold? " << endl;
cin >> size;
vector<int> numbers;
int bnumbers;
for (int count = 0; count < size; count++)
{
cout << "Enter a number: " << endl;
cin >> bnumbers;
numbers.push_back(bnumbers); // Adds an element to numbers
}
//display the numbers stored in order
cout << "The numbers in order are: " << endl;
for(int bcount = 0; bcount < size; bcount++)
{
cout << numbers[bcount] << " ";
}
cout << endl;
//display the numbers stored reversed
cout << "Here are the numbers in reverse order: " << endl;
reverse(numbers.begin(), numbers.end());
for(int rcount = 0; rcount < size; rcount++)
{
cout << numbers[rcount] << " ";
}
cout << endl;
again();
return 0;
}
void again()
{
string answer;
cout << "Would you like to enter another set of data? Y or N?" << endl;
cin >> answer;
string yes = "Yes";
string no = "No";
if(answer == yes)
{
main();
}
}
However, it's a strange things to use recursivity for what you want to do and to call main func multiple times (which is by definition, main entry of a program). In my opinion, you should call in your main function another function named like askInformation which uses a loop with a test case to know if user wants to add informations or not.
This program uses arrays to hold baseball scores for 9 innings. It calculates the high scoring team for each inning and the overall winner of the game.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const int n = 9;
void PrintInput(char[], int[], char[], int[]);
void InningWinner(char[], int[], char[], int[]);
int main()
{
int scores1[n];
int scores2[n];
char team1[n], team2[n];
PrintInput(team1,scores1,team2,scores2);
InningWinner(team1,scores1,team2,scores2);
return 0;
}
void PrintInput(char t1[], int s1[], char t2[], int s2[])
{
cout << "\n********************************************************************\n";
cout << "Team 1: " << t1 << " ";
for (int i = 0; i < n; i++){
cout << setw(5) << s1[i];
}
cout << "\n";
cout << "Team 2: " << t2 << " ";
for (int i = 0; i < n; i++){
cout << setw(5) << s2[i];
}
}
void InningWinner(char t1[], int s1[], char t2[], int s2[])
{
for (int i = 0; i < n; i++){
if (s1[i] > s2[i])
cout << endl << t1 << " Wins Inning " << i + 1 << endl;
else if (s2[i] > s1[i])
cout << endl << t2 << " Wins Inning " << i + 1 << endl;
else if (s1[i] == s2[i])
cout << endl << " Inning " << i+1 << " ends in a TIE" << endl;
}
}
All your arrays are used without explicit initialization, which will produced undefined results.
You need to read values into scores1/2 and teams1/2 before you print them or do calculations. You could read from std::cin as in:
std::cout << "Enter " << n << " scores then press enter: ";
int num_scores_read;
for (num_scores_read = 0; std::cin >> scores1[num_scores_read]; ++num_scores_read)
;
if (!std::cin || num_scores_read < n)
{
std::cerr << "error reading score number " << num_scores_read << '\n';
exit(EXIT_FAILURE);
}
(similar for scores2 etc.)
OR, you could read them from a file (similar to above, but use
#include <fstream>
std::ifstream file(filename);
...as above but use "file" in place of "std::cin"...
OR, just hard code some sample values in your program to get you started:
int scores1[n] = { 1, 3, 5, 1, 3, 5, 4, 5, 3 };