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.
Related
I was given a task to write an example of derived class. But In my program, something strange is happening with the roll numbers.
Also, when this program is compiled in g++.
When I used char [] and gets() to store the strings and input values into them, it didn't allow me to enter the value for collname.
When I use string and cin, I get some strange values while asking for marks.(Check the attached image).
#include<iostream>
#include<conio.h>
using namespace std;
class uni
{
private:
int rollno[100], i, flag;
int intermarks[100];;
int theorymarks[100];
void settheorymarks();
protected:
int numstud;
void setintermarks();
void issurno();
public:
void prepres();
void showres(string collname);
};
class college : public uni
{
private:
string collname;
public:
college(int N)
{
numstud = N;
issurno();
}
void enter_marks();
void disp();
};
void uni::issurno()
{
for (i = 0; i < numstud; i++)
rollno[i] = 1024+i;
cout << "Roll numbers issued!" << endl;
}
void uni::settheorymarks()
{
cout << "Enter theory marks for: " << endl ;
for(i = 0; i < numstud; i++)
{
cout << i+1 << ".) Roll number: " << rollno << " : ";
cin >> theorymarks[i];
}
cout << endl << endl << "Theory marks recorded!" << endl;
}
void uni::setintermarks()
{
cout << "Enter inter marks for: " << endl ;
for(i = 0; i < numstud; i++)
{
cout << i+1 << ".) Roll number: " << rollno << " : ";
cin >> intermarks[i];
}
cout << endl << endl << "Internal marks recorded!" << endl;
}
void uni::prepres()
{
settheorymarks();
}
void uni::showres(string colnam)
{
cout << "College: " << colnam << endl;
cout << "__________________________Result___________________________" << endl;
cout << "s. No.\tRoll no\tInternal\tTheory" << endl;
for(i = 0; i < numstud; i++)
cout << i+1 << "\t" << rollno[i] << '\t' << intermarks[i] << "\t" << theorymarks[i] << endl;
cout << endl << "End of result!" << endl;
}
void college::disp()
{
showres(collname);
}
void college::enter_marks()
{
cout << "Enter the college name: ";
cin >> collname;
setintermarks();
prepres();
}
int main()
{
int n;
cout << "Enter number of stufents: ";
cin >> n;
college c(n);
c.enter_marks();
c.disp();
return 0;
}
I feel that I've made a stupid mistake somewhere.
PS: In school, I was forced to use turbo C++ (One of the oldest compilers).
You just forget the [i] after rollno in the two lines like:
cout << i+1 << ".) Roll number: " << rollno[i] << " : ";
There are a few other things I would ask you to improve if I was your supervisor/teacher:
always use expressive variable and method names. Avoid any abbreviations.
why is there the arbitray offset of 1024 in rollno? If this is just "obfuscation" remove it...
Unclear: why is setintermarks called inside enter_marks but settheorymarks in prepres ?
typo in "stufents"
#pragma once
#ifndef SDDS_GIFT_H
#define SDDS_GIFT_H
#include <iostream>
namespace sdds
{
const int MAX_DESC = 15;
const double MAX_PRICE = 999.999;
const int MAX_WRAP = 20;
struct Gift
{
char m_description[MAX_DESC];
double m_price;
int m_units;
int m_wrapLayers;
struct Wrapping* m_wrap;
};
struct Wrapping
{
char* m_pattern;
};
void gifting(char*);
void gifting(double&);
void gifting(int&);
bool wrap(Gift& theGift);
bool unwrap(Gift& theGift);
void gifting(Gift& theGift);
void display(const Gift& theGift);
}
#endif
<pre><code>
#include <iostream>
#include "Gift.h"
using namespace std;
namespace sdds
{
void gifting(char* m_description) // sending info
{
cout << "Enter gift description: ";
cin.width(MAX_DESC + 1);
cin >> m_description;
}
void gifting(double& m_price)
{
cout << "Enter gift price: ";
cin >> m_price;
while (m_price > MAX_PRICE || m_price < 0)
{
cout << "Gift price must be between 0 and " << MAX_PRICE << std::endl;
cout << "Enter gift price: ";
cin >> m_price;
}
}
void gifting(int& m_units)// gifting function
{
cout << "Enter gift units: ";
cin >> m_units;
while (m_units < 1)
{
cout << "Gift units must be at least 1" << std::endl;
cout << "Enter gift units: ";
cin >> m_units;
};
}
bool wrap(Gift& m_wrap) {
if (m_wrap.m_wrapLayers > 0) {
cout << "Gift is already wrapped!" << endl;
return false;
}
else {
cout << "Wrapping gifts..." << endl;
cout << "Enter the number of wrapping layers for the Gift: ";
cin >> m_wrap.m_wrapLayers;
while (m_wrap.m_wrapLayers < 1) {
cout << "Layers at minimum must be 1, try again." << endl;
cout << "Enter the number of wrapping layers for the Gift: ";
cin >> m_wrap.m_wrapLayers;
}
int i = 0;
m_wrap.m_wrap = new Wrapping[MAX_WRAP + 1];
for (i = 0; i < m_wrap.m_wrapLayers; i++) {
m_wrap.m_wrap->m_pattern = new char[MAX_WRAP + 1];
cout << "Enter wrapping pattern #" << i + 1 << ": ";
cin >> m_wrap.m_wrap->m_pattern;
} // I put struct in a structure
return true;
}
delete[]m_wrap.m_wrap;
m_wrap.m_wrap = nullptr;
}
bool unwrap(Gift& g_unwrap) // unwrap function
{
if (g_unwrap.m_wrapLayers > 0) {
cout << "Gift being unwrapped." << endl;
g_unwrap.m_wrapLayers = 0;
g_unwrap.m_wrap->m_pattern = nullptr;
return true;
}
else
{
cout << "Gift isn't wrapped! Can't unwrap." << endl;
return false;
}
}
void display(const Gift& theGift)
{
cout << "Gift Details:" << endl;
cout << " Description: " << theGift.m_description << endl;
cout << " Price: " << theGift.m_price << endl;
cout << " Units: " << theGift.m_units << endl;
if (theGift.m_wrap == nullptr) // this part seems like a problem
{
cout << "Unwrapped" << endl;
}
else
{
int i = 0;
cout << "Wrap Layers: " << theGift.m_wrapLayers << endl;
for (i = 0; i < theGift.m_wrapLayers; i++) {
cout << "Wrap #" << i + 1 << ": " << theGift.m_wrap[i].m_pattern << endl;
}
}
}
void gifting(Gift& gift) //last function
{
cout << "Preparing a gift..." << endl;
gifting(gift.m_description);
gifting(gift.m_price);
gifting(gift.m_units);
wrap(gift);
}
}
</code></pre>
/***********************************************************************
// Workshop 2: Dynamic Memory & Function Overloading
// Version 2.0
// Date 2020/05/05
// Author Michael Huang
// Description
// Tests Gift module and provides a set of TODOs to complete
// which the main focuses are dynamic memory allocation
//
/////////////////////////////////////////////////////////////////
***********************************************************************/
#include <iostream>
#include "Gift.h"
#include "Gift.h" // intentional
using namespace std;
using namespace sdds;
void printHeader(const char* title)
{
char oldFill = cout.fill('-');
cout.width(40);
cout << "" << endl;
cout << "|> " << title << endl;
cout.fill('-');
cout.width(40);
cout << "" << endl;
cout.fill(oldFill);
}
<pre><code>
int main() {
Gift g1; // Unwrapped Gift
{
printHeader("T1: Checking Constants");
cout << "MAX_DESC: " << sdds::MAX_DESC << endl;
cout << "MAX_PRICE: " << sdds::MAX_PRICE << endl;
cout << "MAX_WRAP: " << sdds::MAX_WRAP << endl;
cout << endl;
}
{
printHeader("T2: Display Wrapped Gift");
gifting(g1.m_description);
gifting(g1.m_price);
gifting(g1.m_units);
cout << endl;
g1.m_wrap = nullptr;
g1.m_wrapLayers = 0;
display(g1);
cout << endl;
}
{
printHeader("T3: Wrap a gift");
if (wrap(g1))
cout << "Test succeeded!";
else
cout << "Test failed: wrapping didn't happen!" << endl;
cout << endl << endl;
}
{
printHeader("T4: Re-wrap a gift");
cout << "Attempting to rewrap the previous Gift: "
<< g1.m_description << endl;
if (wrap(g1) == false)
cout << "Test succeeded!";
else
cout << "Test failed: gift it's already wrapped, cannot wrap again!";
cout << endl << endl;
}
{
printHeader("T5: Unwrap a gift");
cout << "Attempting to unwrap the previous gift: "
<< g1.m_description << endl;
if (unwrap(g1))
cout << "Test succeeded!";
else
cout << "Test failed: you should be able to unwrap!";
cout << endl << endl;
}
{
printHeader("T6: Unwrap again");
cout << "Attempting to un-unwrap the previous gift: "
<< g1.m_description << endl;
if (!unwrap(g1))
cout << "Test succeeded!";
else
cout << "Test failed: you should not be able to unwrap again!";
cout << endl << endl;
}
Gift g2; // Unwrapped Gift
{
printHeader("T7: Prepare another gift");
g2.m_wrap = nullptr;
g2.m_wrapLayers = 0;
gifting(g2);
cout << endl;
display(g2);
cout << endl;
}
{
printHeader("T8: Unwrap the second gift");
unwrap(g2);
}
return 0;
}
Output matches perfectly but I don't know why memory leaks.. please help me. I am doubting my wrap part but I think there must be something else since deallocation seems fine.
I tried my best but I still cannot see which part is wrong.
I cannot see why my deallocation does not work I tried changing it so many times but nothing works.
On this line:
m_wrap.m_wrap->m_pattern = new char[MAX_WRAP + 1];
You allocate memory, but later you only:
delete[]m_wrap.m_wrap;
Also in your for loop you allocate memory, then get some input and store that inside the pointer, as a memory address. Should you ever dereference that, you will invoke undefined behavior, in practice that may likely will a segfault. You should consider rewriting at least that part from scratch.
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
}
This is my .h, header file
#ifndef KINGDOM_H_
#define KINGDOM_H_
namespace westeros {
class Kingdom {
public:
char m_name[32];
int m_population;
};
void display(Kingdom pKingdom[], int kingdomElement, char nameOfKingdom);
}
#endif
This is my .cpp, source file
#include <iostream>
#include "kingdom.h"
using namespace std;
namespace westeros{
void display(Kingdom pKingdom[], int kingdomElement, char nameOfKingdom){
cout << "------------------------------" << endl;
for (int i = 0; i < kingdomElement; i++) {
**if(pKingdom[i].m_name == nameOfKingdom){** //it's giving me error right here, visual studio underlining red line below == sign saying operand types are incompatible
cout << "Searching for kingdom " << pKingdom[i].m_name << " in Westeros " << endl;
cout << "------------------------------" << endl;
cout << pKingdom[i].m_name << ", population " << pKingdom[i].m_population << endl;
}
else {
cout << "------------------------------" << endl;
cout << "Searching for kingdom " << nameOfKingdom << " in Westeros " << endl;
cout << "------------------------------" << endl;
cout << nameOfKingdom << " is not part of Westeros." << endl;
cout << "------------------------------" << endl;
}
}
}
}
and this is my main file trying to call it
#include <iostream>
#include "kingdom.h"
using namespace std;
using namespace westeros;
int main(void)
{
int count = 0; // the number of kingdoms in the array
Kingdom* pKingdoms = nullptr;
//allocating dynamic memory
pKingdoms = new Kingdom[count];
display(pKingdoms, count, "Mordor");
cout << endl;
display(pKingdoms, count, "The_Vale");
cout << endl;
delete[]pKingdoms;
pKingdoms = nullptr;
return 0;
}
Can anyone find what could be the problem?
Your problem is that pKingdom[i].m_name is a char[32], and the type of nameOfKingdom is char. You cannot compare a character array with a character.
which type would I use then?
std::string
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.