I\'m not sure how to nullpoint string! I wrote this code and aparently the name
ID: 3567390 • Letter: I
Question
I'm not sure how to nullpoint string!
I wrote this code and aparently the name initializes with "0"
how can I fix it???
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
long numbersOFvendors();
string *getNames(int);
int *getIncome(int);
int *AmountDue(int[], int);
void displayArray(string[], int[], int[], int);
int main()
{
int size = numbersOFvendors();
string *names = nullptr;
int *income = nullptr;
int *amountdue = nullptr;
names = getNames(size);
income = getIncome(size);
amountdue = AmountDue(income,size);
displayArray(names, income, amountdue, size);
delete[] names;
delete[] income;
delete[] amountdue;
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;
}
int *AmountDue(int temp[], int n)
{
int *amount=nullptr;
amount = new int[n];
for (int i = 0; i < n; i++)
{
amount[i] = temp[i] / 10;
}
return amount;
}
void displayArray(string names[], int income[], int amountdue[], int size)
{
cout << " Vendor name today's income Amount due ";
for (int c = 0; c < size; c++)
cout << names[c] << " " << income[c]
<< " " << amountdue[c] << endl;
}
Explanation / Answer
#include <stdafx.h>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
long numbersOFvendors();
string *getNames(int);
int *getIncome(int);
int *AmountDue(int[], int);
void displayArray(string[], int[], int[], int);
int main()
{
int size = numbersOFvendors();
string *names = NULL;
int *income = NULL;
int *amountdue = NULL;
names = getNames(size);
income = getIncome(size);
amountdue = AmountDue(income,size);
displayArray(names, income, amountdue, size);
delete[] names;
delete[] income;
delete[] amountdue;
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 = NULL;
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;
}
int *AmountDue(int temp[], int n)
{
int *amount=NULL;
amount = new int[n];
for (int i = 0; i < n; i++)
{
amount[i] = temp[i] / 10;
}
return amount;
}
void displayArray(string names[], int income[], int amountdue[], int size)
{
cout << " Vendor name today's income Amount due ";
for (int c = 0; c < size; c++)
cout << names[c] << " " << income[c]
<< " " << amountdue[c] << endl;
}