user1793058
user1793058

Reputation: 11

How to format an array and output?

Ok need some help. I have read in a file to an array. I need to take the array and format it so that the numbers are formatted. The text file is just a list of numbers. For example, I need to take the first two numbers and format them so that it is printed out to the console like this: "1-0". Here is my code so far. Notice the incomplete function where the formatting should take place.

#include<iostream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<fstream>

using namespace std;

string ArrayFormat(int array[]);

int main() {

const int ARRAY_SIZE = 21;
string filename;
ifstream inputfile;

int score[ARRAY_SIZE];


cout <<"\nPlease enter the name of a file: ";
cin >> filename;

inputfile.open(filename.c_str());
if (inputfile.fail()){
    perror(filename.c_str());
    exit(1);
}// end of error test

for (int i=0;i<20;i++){
    inputfile >> score[i];
   // cout << score[i] << endl;
}// end of for loop to read into array
inputfile.close();


}// end of main


string ArrayFormat(int array[]){


for(int i=0; i<=21; i++){

}


}// end of ArrayFormat

Upvotes: 1

Views: 4066

Answers (1)

Syntactic Fructose
Syntactic Fructose

Reputation: 20104

for(int i = ; i < 21; i++)
{
   if(i%2 == 1) 
      std::cout << "-" << array[i] << "\t";
   else
      std::cout << array[i];
}

this will print the array in pairs of 2's, with the correct format.

EDIT: Why is the array odd if you need to print out pairs of numbers?

Upvotes: 1

Related Questions