I am trying to figure out what I am doing wrong here and its five in the morning and I have to turn this in today at 9 in the morning, so this is my last hope to come to you guys.
This program has to allow a user to enter 5 ints, 7 floats and 5 chars, which go into arrays which have to be coded via Function Templates. The numeric data than has to be bubble sorted and averaged. This has to be printed out Then I have to save the arrays to a .dat file and retrieve the data from the file and once again output it.
Errors: The Average is not recorded properly somehow so you get a nasty random memory output.
Only the int values are being saved to the text file, and I don't even know how to retrieve properly and print the retrieved values again.
Updated Code with fixed calc function
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
//Load data to array from Keyboard.
template <class T>
void load(T *a,const int n)
{
for(int i=0;i<n;i++)
cin>>a[i];
}
//Calc and Print the Average for a numeric array.
template <class T>
void calc(T *a,const int n,float *avg)
{
float b=0;
for (int i=1;i<n;i++)
{
b+=a[i];
}
*avg=b/n;
cout<<"The Average is: "<<*avg<<endl;
//Does not work. Prints out a random block of memory.
//Tried a couple things and still get he same bug.
}
//Sort the data array in ascending order.
template <class T>
void sort(T *a,const int n)
{
float t;
for(int i=0;i<n-1;i++)
for(int j=0;j<n-1;j++)
if (a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
//Save the array data to a text file.
template <class T>
void get(T *a,const int n)
{
ofstream outfile("C:\array.dat", ios::out);
for(int i=0;i<n;i++)
outfile << a[i]<< endl;
outfile.close();
//Only saves the first array (The ints)
}
//Retrieve the array data from the text file.
template <class T>
void save(T *a,const int n)
{
ifstream infile("C:\array.dat", ios::in);
for(int i=0;i<n;i++)
{
infile>>a[i];
}
infile.close();
}
/*Output should include the average for each of the two numeric arrays
along with all three arrays being printed out in ascending order twice,
once before the text file is saved and once after the array is retrieved
from the text file*/
int main()
{
const int n1=5,n2=7,n3=5;
int a[n1];
float b[n2];
char c[n3];
float avg[3];
int i;
cout<<"Enter 5 integers"<<endl;
load(a,n1);
sort(a,n1);
calc(a,n1,avg);
cout<<"Enter 7 floats"<<endl;
load(b,n2);
sort(b,n2);
calc(b,n2,avg+1);
cout<<"Enter 5 strings"<<endl;
load(c,n3);
cin.ignore(20, '\n');
sort(c,n3);
cout << endl;
cout<<"Output:"<<endl;
cout<<"The Integer array:" << endl;
for (i = 0; i < n1; i++)
cout << a[i] << " ";
cout << endl;
cout<<"The Float array:" << endl;
for (i = 0; i < n2; i++)
cout << b[i] << " ";
cout << endl;
cout<<"The String array:" << endl;
for (i = 0; i < n3; i++)
cout << c[i] << " ";
cout << endl;
save(a,n1);
get(a,n1);
save(b,n2);
get(b,n2);
save(c,n3);
get(c,n3);
//Need to print the now returned values here.
cout << endl;
//cin.get();
system("PAUSE");
return 0;
}
Sorry if half of this doesn't make sense. I am really tired.
For the average:
template <class T>
void calc(T *a,const int n,float *avg)
{
float b=0;
for (int i=1;i<n;i++)
{
b+=a[i];
}
avg[n]=b/n;
cout<<"The Average is: "<<avg[4]<<endl;
//Does not work. Prints out a random block of memory.
//Tried a couple things and still get he same bug.
}
you don't need an array of averages, you just want one average.
agv[n] = b / n; //assigns to the the n'th elemement of an array called avg.
try this instead:
*avg = b/n
And for printing it out, just '*avg' also.
When you call calc, try:
float avg1;
calc(b,n4,&avg1);
When you see & and *, read them as the following:
& address of
* contents of address
you only need three averages, one for the 5 ints, one for the 7 floats and one for the 5 chars.
float avg[4]; // should be: float avg[3];
You seem to be passing the wrong parameters to each of the calc functions. When you call calc for the ints, use:
calc(a,n1,avg);
for floats:
calc(b,n2,avg+1);
for chars:
calc(c,n3,avg+2);
n4=2; is not relevant to anything, you can delete n4.
When you open the file, you will need to specify ios:app which appends to the existing file. Without that, every time you write to the file anything that existed in it before will be thrown away.
ios::out | ios::app
So, first write just use ios::out, after that include ios::app
Related
I'm writing two functions: one of them is for "filling" array with random values and int the second function I have to use the same array, choose one row and find the min element of that row.
But the problem is that I don't know how to pass values from one function to another.
Here is my code:
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
void fillarray(int arr[5][5], int rows, int cols) {
cout << "Static Array elements = \n\n" << flush;
for(int i = 0; i < rows; ++i) {
cout << "Row " << i << " ";
for(int j = 0; j < cols; ++j) {
arr[i][j] = rand() % 10;
cout << arr[i][j] << " " << flush;
}
cout << endl;
}
cout << " \n\n";
}
void minarray(int a, void fillarray) { // don't know what to write here
there:
int min = INT_MAX; // Value of INT_MAX is 2147483648.
if(a > 4) {
cout << "Invalid input! " << endl;
goto there;
}
for(int counter = 0; counter < 5; ++counter) {
if(arr[a][counter] < min) min = arr[a][counter];
}
cout << "Minimum element is " << min << endl;
}
int main() {
int z;
srand(time(NULL));
const int rows = 5;
const int cols = 5;
int arr[rows][cols];
fillarray(arr, rows, cols);
cout << "Enter the number of row: ";
cin >> z;
minarray(z, fillarray)
system("PAUSE");
}
For starters the function fillarray has redundant parameter cols because this number is known from the declaration of the first parameter int arr[5][5].
Th function can be declared like
void fillarray(int arr[5][5], int rows )
You could supply the parameter cols in case when not the whole array is filled in the function.
You already filled the array by this call
fillarray ( arr, rows, cols );
The function performed its task. So there is no need to reference the function one more time as you are trying
minarray(z, fillarray)
The function minarray can be declared either like
void minarray( const int arr[], size_t n );
and called like
minarray( arr[z], cols );
with a preliminary check that z is less than 5.
Or it can be declared like
void minarray( const int arr[][5], size_t n, size_t row );
and called like
minarray( arr, rows, z );
Pay attention to that there is the standard algorithm std::min_element that allows to find minimum element in an array. And to fill an array with values you can use the standard algorithm std::generate.
And each function should do only one task. For example the function fillarray should silently fill the array with values. To output the array you could write a separate function.
I'm not sure this even compiles, but i'm guessing you want to pass int arr[x][y] from the fill Array function to the minArray function. To do that you first need to include arr as a parameter of minArray. From there you need to pass it by reference. Then, you can call minArray from fillArray.
What you need to do is call fillarray to fill your array. So it would look like
fillarray(arr, rows, cols);
Just like you have so far. Now, you have array arr all filled in. minarray doesn't care how that happened. So don't pass it your filler method. Pass it the array.
minarray(cols, arr[z]);
You don't need to pass the entire array -- just the row in question. You're also passing the width.
And change the definition of minarray:
void minarray(int length, int[] array)
Now, your minarray itself needs changes. First, get rid of the if-check. You don't need to pass a row number now, but you do need the number of columns passed as length.
Then your for loop looks like:
for (int index = 0; index < length; ++index) {
if (array[index] < min) {
min = array[index];
}
}
So, to summarize:
Main declares the data and calls your two methods.
fillarray populates the array. It is called from main the way you already have.
minarray prints the minimum on a single line. It is also called from main, passing in the array, not the method that filled it.
You have one more issue, however. fillarray hardcodes the array size as 5x5, but main uses constants defined. I'd move those contents to the top of the file and use them in both places.
Move to the top, below any #includes:
const int rows = 5;
const int cols = 5;
Define fillarray:
void fillarray(int arr[rows][cols]) {
And when you call it from main:
fillarray(arr);
I'll let the other answers answer your question and concentrate on the code around your goto that you asked about in the comments.
In main you have this:
cout << "Enter the number of row: ";
cin >> z;
minarray(z, fillarray)
In minarray you have this:
void minarray(int a, void fillarray) { // don't know what to write here
there:
int min = INT_MAX; // Value of INT_MAX is 2147483648.
if(a > 4) {
cout << "Invalid input! " << endl;
goto there;
}
First, there's absolutely no reason to use goto. You could do this:
void minarray(int a, void fillarray) { // don't know what to write here
int min = INT_MAX; // Value of INT_MAX is 2147483648.
while(a > 4) { // loop for as long as "a > 4"
cout << "Invalid input! " << endl;
}
Removing the goto made the bug rather apparent. a will never change inside the loop, so it'll just print Invalid input! forever if you give it invalid input. An alternative would be to validate the input when you actually get the input from the user (in main):
while(true) { // loop forever
cout << "Enter the number of row: ";
if(cin >> z) { // check that the user inputs an int
if(z<0 || z>4) // validate the input
cout << "Invalid input!\n";
else
break; // we got valid input, break out of the while loop
} else { // user did not input an int
std::cout << "input failed - aborting\n";
return 1; // return from main to exit the program
}
} // if the program reaches this point, it'll ask the user for input again
// and that will only happen if the user gives it an int that is <0 or >4
I am writing a program that will receive a text file, read in a list of names from that file, sort those names in alphabetical order, and then print the sorted list back out.
This originally was a project for my "Intro to Programming" class that was to use arrays, but I'm trying to retool it to work with vectors instead, allowing me to use a file of any length instead of a rigid array length.
But Intellisense is giving me an error:
No suitable conversion function from "std::vector<std::string, std::allocator<std::string>>" to "std::vector<std::string, std::allocator<std::string>>*" exists
Any time that I try to pass the filename to a function (ex at displayArray(names, nameQty), I get the error with names). I'm having a hard time googling how to pass a vector to a function, so I think that's where my problem is. The full code is pasted below:
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
int readFromFile(string[], string);
void displayArray(vector<string>[], int);
void alphaSort(vector<string>[], int);
void swap(string&, string&);
int main() {
//Declare variables
string fileName;
vector<string>names(1);
int nameQty;
//Prompt for file name
cout << "Please enter the name of the file to read names from: " << endl;
cin >> fileName;
//Call function to open file and read names into a vector array. Function will return the number of names in file
nameQty = readFromFile(names, fileName);
//Display unsorted names
cout << "Unsorted names:" << endl;
displayArray(names, nameQty);
//Sort names into alphabetical order
alphaSort(names, nameQty);
//Display sorted names
cout << "Sorted names:" << endl;
displayArray(names, nameQty);
//More to come after this; program isn't done yet!
}
/*
* Function to read a list from a text file into an array.
* The array starts at size 1, then increments by 1 for each subsequent iteration
* The array then deletes the final element when there is nothing more to read
* (since the last element will be uninitialized)
*/
int readFromFile(vector<string> array, string fileName) {
ifstream inputFile;
inputFile.open(fileName);
if (!inputFile) {
cout << "Invalid file name. Please restart program and try again."
<< endl;
system("pause");
exit(EXIT_FAILURE);
}
else {
int index = 0;
while (inputFile) {
cin >> array[index];
array.push_back;
index++;
}
array.pop_back;
inputFile.close();
return (index + 1);
}
}
//Function to display list of items in array
void displayArray(vector<string> array[], int quantity) {
for (int i = 0; i < quantity; i++)
cout << array[i] << endl;
}
//Selection sort function puts array elements in alphabetical order
void alphaSort(vector<string> names[], int qty) {
for (int j = 0; j < qty - 1; j++) {
for (int i = j + 1; i < qty; i++) {
if (names[j] > names[i]) {
swap(names[j], names[i]);
}
}
}
}
//Function to swap elements a and b in array
void swap(string &a, string &b) {
string temp = a;
a = b;
b = temp;
}
Please don't worry about my using system("pause") and using namespace std. I'm aware that's poor practice, but it's what we've been asked to do in class.
You want:
void displayArray(vector<string>& array, int quantity)
or in this case even better:
void displayArray(const vector<string>& array, int quantity)
In your code it seems you are trying to pass an array of vectors (which is not that straightforward in C++ and boils down to a pointer to a vector in this case). To avoid copying you should use a reference.
You may also be interested to read about move semantics.
Also, as noted in the comments, you may also want to fix readFromFile:
int readFromFile(vector<string>& array, string fileName)
By changing it to a reference the function will modify caller's vector, instead of creating and modifying a local copy.
this is my first time posting on here, and I may not be doing this the right way, sorry in advance.
I am having trouble understanding how to copy two separate one dimensional arrays into one multidimensional array. It is part of my assignment, but I already turned it in and am probably going to lose points in it, but it's ok. As long as I get to understand how it's done, I'd still be happy.
The specific part I am having trouble with is section 5, I got everything else down, maybe it's because its been a long day, but I just can't seem to get it. I will post the whole assignment as well as my code. Thank you for taking the time to read all this if you did.
Write a C++ program that tests the function main and the functions discussed in parts 1 through 7. (Add additional functions, such as printing a two-dimensional array, as needed.).
Consider the following function main:
<code>int main()
{
int inStock[10][4];
int alpha[20];
int beta[20];
int gamma[4] = {11, 13, 15, 17};
int delta[10] = {3, 5, 2, 6, 10, 9, 7, 11, 1, 8};
.
.
.
}</code>
Write the definition of the function setZero that initializes any one-dimensional array of type int to 0 (alpha and beta).
Write the definition of the function inputArray that prompts the user to input 20 numbers and stores the numbers into alpha.
Write the definition of the function doubleArray that initializes the elements of beta to two times the corresponding elements in alpha. Make sure that you prevent the function from modifying the elements of alpha.
Write the definition of the function copyGamma that sets the elements of the first row of inStock from gamma and the remaining rows of inStock to three times the previous row of inStock. Make sure that
you prevent the function from modifying the elements of gamma.
5. Write the definition of the function copyAlphaBeta that stores alpha into the first five rows of inStock and beta into the last five rows of inStock. Make sure that you prevent the function from modifying the
elements of alpha and beta.
Write the definition of the function printArray that prints any one-dimensional array of type int.
Write the definition of the function setInStock that prompts the user to input the elements for the first column of inStock. The function should then set the elements in the remaining columns to two times the
corresponding element in the previous column, minus the corresponding element in delta.
Here is my code:
<code>
#include <iostream>
using namespace std;
int setZero(int alpha[], int beta[]);
int inputArray(int alpha[]);
int doubleArray(const int alpha[], int beta[]);
int copyGamma(const int gamma[], int inStock[][4]);
void copyAlphaBeta(int inStock[][4], const int numberOfRows, const int
alpha[], const int beta[], int numSize);
void printArray(int print[], int n);
int setInStock(int inStock[][4],const int delta[]);
int main()
{
int inStock[10][4];
int alpha[20];
int beta[20];
int gamma[4]={11,13,15,17};
int delta[10]={3,5,2,6,10,9,7,11,1,8};
setZero(alpha, beta);
cout << "Alpha after initialization" << endl;
printArray(alpha, 20);
inputArray(alpha);
cout << "Alpha after reading 20 numbers" << endl;
printArray(alpha, 20);
doubleArray(alpha, beta);
cout << "Beta after a call to doubleArray" << endl;
printArray(beta, 20);
copyGamma(gamma, inStock);
copyAlphaBeta(inStock, 10, alpha, beta, 20);
for (int row=0; row<10;row++)
for (int col=0; col<4; col++)
{
if (col%4==0)
cout << endl;
cout << inStock[row][col] << '\t';
}
cout << endl;
setInStock(inStock, delta);
return 0;
}
int setZero(int alpha[], int beta[])
{
for (int i=0; i<20; i++)
{
alpha[i]=0;
beta[i]=0;
}
cout << endl;
}
int inputArray(int alpha[])
{
cout << endl;
cout << "Enter 20 integers" << endl;
for (int i=0; i<20; i++)
{
cin >> alpha[i];
}
cout << endl;
return 0;
}
int doubleArray(const int alpha[], int beta[])
{
cout << endl;
for (int i=0; i<20; i++)
{
beta[i]=alpha[i]*2;
}
cout << endl;
return 0;
}
int copyGamma(const int gamma[], int inStock[][4])
{
cout << endl;
cout << "inStock after a call to copyGamma" << endl;
for (int row=0;row<10;row++)
for (int column=0;column<4;column++)
{
if (row==0)
inStock[row][column]=gamma[column];
else
inStock[row][column]=3*inStock[row-1][column];
}
cout << endl;
for (int r=0;r<10;r++)
for (int c=0; c<4;c++)
{
if (c==0)
cout << endl;
cout << inStock[r][c] << '\t';
}
cout << endl;
return 0;
}
void copyAlphaBeta(int inStock[][4], const int numberOfRows, const int
alpha[], const int beta[], int numSize)
{
cout << endl;
cout << "inStock after a call to copyAlphaBeta" << endl;
for(int r=0;r<numberOfRows;r++)
{
for(int c=0;c<4;c++)
{
for (int counter=0; counter<numSize; counter++)
{
if(r<5)
{
inStock[r][c]=alpha[counter];
}
else if (r>=5)
{
inStock[r][c]=beta[counter];
}
}
}
}
cout << endl;
}
void printArray(int print[], int n)
{
cout << endl;
for (int i=0;i<n;i++)
cout << print[i] << " ";
cout << endl;
}
int setInStock(int inStock[10][4],const int delta[])
{
int input;
cout << endl;
cout << "Enter 10 integers" << endl;
for (int r=0;r<10;r++)
for (int c=0;c<4;c++)
{
if(c==0)
{
cin >> inStock[r][c];
}
else
inStock[r][c] = 2 * inStock[r][c-1] - delta[r];
}
for (int row=0; row<10;row++)
for (int col=0; col<4; col++)
{
if (col%4==0)
cout << endl;
cout << inStock[row][col] << " ";
}
}
TL;DR - Need help understanding past homework, I just want to know the proper way to do it. Also please critique, looking for ways to improve. Thank you!
Edit:
The problem I have is with this part:
Write the definition of the function copyAlphaBeta that stores alpha into the first five rows of inStock and beta into the last five rows of inStock. Make sure that you prevent the function from modifying the
elements of alpha and beta.
and my code relating to this portion of the question is:
<code>
void copyAlphaBeta(int inStock[][4], const int numberOfRows, const int
alpha[], const int beta[], int numSize)
{
cout << endl;
cout << "inStock after a call to copyAlphaBeta" << endl;
for(int r=0;r<numberOfRows;r++)
{
for(int c=0;c<4;c++)
{
for (int counter=0; counter<numSize; counter++)
{
if(r<5)
{
inStock[r][c]=alpha[counter];
}
else if (r>=5)
{
inStock[r][c]=beta[counter];
}
}
}
}
cout << endl;
}</code>
Thank you for the links, I am not allowed to use vectors at the moment, but I am definitely going to study up on them.
First of all, if you're allowed to use std::vector you should use it. Read up on std::vector here for more details.
Vector provides a dynamically allocated (in the back) chunk of memory that can "expand" and change at runtime depending on adding/deleting elements. You might want to learn to use a vector as they are very useful.
However, you don't really need a vector to do your copying. You can use std::copy to do this for you. More info for it here: std::copy
You can use it to take 3 inputs for your case. First two are the beginning and end pointers, the last input is the starting pointer for the destination. You should be able to figure out what they are for your project.
In your nested for-loops, the last loop is wrong and it will repeatedly put different values of alpha[count] and beta[count] into a same inStock[r][c] element.
You can remove this loop and change the code like
for(int r=0;r<numberOfRows;r++)
{
for(int c=0;c<4;c++)
{
if(r<5)
{
inStock[r][c]=alpha[c]; // assuming size of alpha is 4
}
else if (r>=5)
{
inStock[r][c]=beta[c]; //assuming size of beta is 4
}
}
}
In this code, you have assumed inStock has 4 columns, these 4 columns of each row, are going to be filled with 4 elements of alpha and beta arrays.
So I'm new to C++ and am writing a program to calculate the mean, median, standard deviation, min, and max of a program. The issues I'm having are that I don't know how to correctly call functions from other files in the project, and when I try to print(c out <<) my array, it returns a weird value like 0 x 0017 c 530 (disregard the spaces). If anyone could help me correctly return these functions, and print the list correctly, I would be very grateful! Here's my code (not including the .h file):
#include <iostream>
using namespace std;
#include "stats.h"
int main()
{
double nums[10000] = {};
int n;
cout << "Enter number of values: ";
cin >> n;
for (int i = 1; i <=n; i++) {
cout << "Enter number " << i << ": ";
cin >> nums[i-1];
}
// me trying to print the nu ms list
cout << nums << endl;
// me trying to call the function
double mean(double nums[], int n);
return 0;
}
stats. cpp
#include "stats.h"
double mean(double nums[], int n)
{
double sum = 0;
double average;
for (int i = 0; i > n; i++) {
sum += nums[i];
}
average = sum / n;
return average;
}
Instead of
cout << nums << endl;
Just like you have a loop to enter one value at a time in an array, you also need a similar loop to print one value at a time from the array.
To call a function from another translation unit, you would typically declare the function in a header file, your stats.h would be an excellent candidate:
double mean(double nums[], int n);
And then just invoke it from your main:
std::cout << mean(nums, n) << std::endl;
That's it.
Also:
using namespace std;
You need to have someone help you to get an amnesia, and completely forget that C++ has anything like this. This is bad programming practice.
double mean(double nums[], int n); is declaration of function, not invoking of function. You should
double mean_value = mean(nums, n);
cout << mean_value << endl;
And cout << nums << endl; won't print out the elements of the array, just the address of the array. You need to loop the array to print out all the elements.
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
const int NUMBER = 20;
void dataRead(double a[], int size, double& data);
double averageArray(const double a[], double data);
double totalArray(const double a[], double data);
double highCost(const double a[], double data);
double lowCost(const double a[], double data);
int main()
{
// Calling in the file
ifstream in;
char in_file[16];
double data;
double averageSales, higherSale, lowerSale, totalSales, salesData[NUMBER];
cout << "Enter the input file name." << endl;
cin >> in_file;
in.open(in_file);
// Incase the file doesnt open
if(in.fail())
{
cout << "File not found." << endl;
exit(1);
}
// For calling the answers from the formula functions
totalSales = totalArray(salesData, data);
cout << endl << "The total sales are $" << totalSales << endl;
averageSales = averageArray(salesData, data);
cout << "The average sales amount is $" << averageSales << endl;
higherSale = highCost(salesData, data);
cout << "The highest sales amount is $" << higherSale << endl;
lowerSale = lowCost(salesData, data);
cout << "The lowest sales amount is $" << lowerSale << endl;
return 0;
}
// Formula functions used for values
double totalArray(const double sales[], double n)
{
double sum = 0;
for(int count = 0; count < n; count++)
sum += sales[count];
return(sum);
}
double averageArray(const double sales[], double n)
{
return((double)totalArray(sales, n));
}
double highCost(const double sales[], double n)
{
double highest;
highest = sales[0];
for(int count = 1; count < n; count++)
if(highest < sales[count])
highest = sales[count];
return(highest);
}
double lowCost(const double sales[], double n)
{
double lowest;
lowest = sales[0];
for(int count = 1; count < n; count++)
if(lowest > sales[count])
lowest = sales[count];
return(lowest);
}
void readData(int a[], int size, double& data)
{
}
I am having trouble getting my program to read files and calculate the data for answers.. It is supposed to take in a file and return specific values pertaining to what is in the file. Like the average, highest cost value, lowest cost value, and total values together. I am compiling this on c9.io and when I added a new file to grab the information from no matter what values I put in the file it just gives me zeros or weird numbers with remainders. Would appreciate some guidance! Thank you!
You open the file, but never read anything from it (nor close it, for that matter). You read from in in a similar way that you read from in, and this describes how to close: How to close IStream?.
I don't see a line that would actually read the data in the file, you're just opening it.
How to read the contents of a file
If your file has just plain numbers and whitespace in it, like "69 10 38 33 (...)", you'll want to read them like that:
int number;
file>>number;
cout<<number<<endl;
file>>number;
cout<<number<<endl;
- the example program will output the following:
69
10
When your file is written to be more "human-readable", like:
Value1 - 69
Value2 - 10
etc.
it gets tricky. I would recommend just changing the file to the " 69 10 ..." format. If you really want to keep it like that, you could read the values to a string - just as you would with numbers - and check if it's numerical. If yes, convert it to an int using stringstream. These 2 functions may help you
#include <sstream>
int StringToInt(string input)
{
int res;
stringstream ss(input);
ss>>res;
return res;
}
bool IsNumeric(string input)
{
for(int i=0; i<input.size(); i++)
{
if(input[i] < '0' && input[i] > '9')
return false;
}
return true;
}
And by the way: you can't pass arrays to a function like you tried to do.