1

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
3
  • What format are they initially being stored into the array? Please clarify exactly what you're in need of. Commented Nov 2, 2012 at 2:01
  • 1
    What happens to the rest of the numbers beyond the second one? Commented Nov 2, 2012 at 2:02
  • Initially, they are stored as a list one number per line. Commented Nov 2, 2012 at 2:44

1 Answer 1

1
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?

Sign up to request clarification or add additional context in comments.

5 Comments

The last set of numbers is actually 3 numbers 1-3-0. That is why it is 21.
This is really close. However, they need to be printed out on the same line. This prints one per line. And I didn't mention that the last set of numbers is 3 numbers. So the last index should be 1-1-1. Any advice how I can add a tab "\t" between each formatted output and have them on the same line?
will the last set always be 3 numbers? I don't understand why you want all the numbers to be displayed in pairs of two except for the last three numbers, if it is only the last three then that would be easy to fit in, i changed the code to be more efficient and loop through each variable.
Yes the last set will always be 3 numbers. I attempted to use the edited code and while it is close to what I need, it does not work. The output is as follows: -1 1-2 4-5 0-8 1-4 0-3 2-2 5-3 1-4 3-8 1-4 3883031360. When the output should be 1-1 2-4 5-0 8-1 4-0 3-2 2-5 3-1 4-3 8-1 4-3-3. The tabs do not carry over in the post here so sorry if it is jumbled.
fixed the code, it should print all of the array in pairs of two. Making the last pair three would simply be adding a conditional statement checking if the array is at its end, in which case you add another '-' instead of '-' and tabbing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.