I debuged however the vendor name initialize with null. How can I fix it?( I ass
ID: 3567389 • Letter: I
Question
I debuged however the vendor name initialize with null.
How can I fix it?( I assume that I did wrong with nullpointing string
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
long numbersOFvendors();
string *getNames(int);
int *getIncome(int);
int main()
{
int size = numbersOFvendors();
string *names = nullptr;
int *income = nullptr;
names = getNames(size);
income = getIncome(size);
cout << " Vendor name today's income ";
for (int c = 0; c < size; c++)
cout << names[c]<< " " << income[c] << endl;
delete[] names;
delete[] income;
names = 0;
income = 0;
return 0;
}
long numbersOFvendors()
{
int temp;
cout << "How many vendors? ";
cin >> temp;
while (temp < 1)
{
cout << "Entered number is zero or negative.";
cin >> temp;
}
return temp;
}
string *getNames(int n)
{
string *NameArr = new string[n];
cout << "Vendors' name?" << endl;
for (int i = 0; i < n; i++)
{
cout << (i + 1) << ": ";
getline(cin, NameArr[i]);
while (NameArr[i].empty())
{
cout << "Name cannot be null.";
getline(cin, NameArr[i]);
}
}
return NameArr;
}
int *getIncome(int n)
{
int *incomeArr = nullptr;
incomeArr = new int[n];
cout << "How much did each vendor make?" << endl;
for (int i = 0; i< n; i++)
{
cout << (i + 1) << ": ";
cin >> incomeArr[i];
while (incomeArr[i] < 0)
{
cout << "Entered value is negative.";
cin >> incomeArr[i];
}
}
return incomeArr;
}
Explanation / Answer
Here you go :)
//you just need to remove the nullptr initilization. There is no keyword called 'nullprt'. Just make it NULL or remove it. Hope it helps!
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
long numbersOFvendors();
string *getNames(int);
int *getIncome(int);
int main()
{
int size = numbersOFvendors();
string *names;
int *income;
names = getNames(size);
income = getIncome(size);
cout << " Vendor name today's income ";
for (int c = 0; c < size; c++)
cout << names[c]<< " " << income[c] << endl;
delete[] names;
delete[] income;
names = 0;
income = 0;
return 0;
}
long numbersOFvendors()
{
int temp;
cout << "How many vendors? ";
cin >> temp;
while (temp < 1)
{
cout << "Entered number is zero or negative.";
cin >> temp;
}
return temp;
}
string *getNames(int n)
{
string *NameArr = new string[n];
cout << "Vendors' name?" << endl;
for (int i = 0; i < n; i++)
{
cout << (i + 1) << ": ";
getline(cin, NameArr[i]);
while (NameArr[i].empty())
{
cout << "Name cannot be null.";
getline(cin, NameArr[i]);
}
}
return NameArr;
}
int *getIncome(int n)
{
int *incomeArr;
incomeArr = new int[n];
cout << "How much did each vendor make?" << endl;
for (int i = 0; i< n; i++)
{
cout << (i + 1) << ": ";
cin >> incomeArr[i];
while (incomeArr[i] < 0)
{
cout << "Entered value is negative.";
cin >> incomeArr[i];
}
}
return incomeArr;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.