How to declare the array and read the array? Must I use data structure?

Two array

1 30
2 20
3 10
4 50
5 15

Dani AI

Generated

The file you showed is just two columns of integers (id value). The main problems in the posted code are simple and common: the read-index i is never initialized, the same name myfile is reused for an output stream (shadowing the input stream), loops use array contents as loop bounds instead of the actual count read, and comparisons like exams[i+1] or …[j+1] will go out of bounds on the last element. pointed to the right idea (store rows, then iterate by the number actually read); follow that flow and the program will behave predictably.

Recommended, safe workflow (step by step)

  • Open the input file and check it opened successfully.
  • Read pairs until extraction fails, storing them in a dynamic container so you don’t need to guess maximum rows.
  • Use the number of rows actually read (container.size()) as your loop bound.
  • Sort using a standard algorithm (std::sort with a comparator) instead of hand-rolling a bubble with off‑by‑one errors.
  • Don’t reuse the same variable name for different streams; return 0 from main.

Example (concise, safer approach)

std::vector<std::pair<int,int>> rows;
int a,b;
std::ifstream in("STA83SOLUTION.txt");
if (!in) /* handle error */;
while (in >> a >> b) rows.emplace_back(a,b);
std::sort(rows.begin(), rows.end(), [](auto const& x, auto const& y){ return x.second > y.second; });
for (auto const &p : rows) std::cout << p.first << " " << p.second << '\n';

Quick debugging checklist

  • Initialize counters (e.g., int i = 0;) before use.
  • Print the row count after reading to verify you actually read data.
  • Watch for shadowed variables (same name in inner scope).
  • If you must use a raw array, keep a separate count and use it for all loops.
    Following those fixes will resolve the “no output” symptom and avoid the sorting/indexing crashes seen in the thread.

Recommended Answers

All 14 Replies

yes...but it was so complicated..i want to read as simple as possible.

I'm having problem when i want to cout the data that i sort.

int temp,bubsort,arr,display;
	if (myfile.is_open())
	{
		for (int i = 0; i < exams.size(); i++) 
		{
			for (int j = 0; j<exams.at(i).total.size(); j++) 
			{
		cout<<"\n"<<i+1<<":"<<" "<< exams[i].total[j]<<"\t"; // output list of exam codes for this student
	  
	if (exams[i+1].total[j+1]>exams[i].total[j])
	
	{	
		temp=exams[i+1].total[j+1];
		exams[i+1].total[j+1]=exams[i].total[j];
		exams[i].total[j]=temp;	

	}
	bubsort(arr,j);
	display(arr,j);
	cout<<endl<<endl;

	}		
	}	
	}
cin.get();
return 0;
}
}

you should know by now how to open and read the file. All you have to do is make a 2d array of integers int array[255][2] = {0}; That will hold up to 255 rows of data

.......it's not hard at all.

(edit: lol a little late, sorry I was reading the other thread)

Just change it like this to make 2d array?

int array[140][2]={0};
int temp;
if (myfile.is_open())
{
for (int i = 0; i < exams.size(); i++) 
{
for (int j = 0; j<exams.at(i).total.size(); j++) 
{
cout<<"\n"<<i+1<<":"<<" "<< exams[i].total[j]<<"\t"; // output list of exam codes for this student
if (exams[i+1].total[j+1]>exams[i].total[j])
{	
temp=exams[i+1].total[j+1];
exams[i+1].total[j+1]=exams[i].total[j];
exams[i].total[j]=temp;	
}

>>Just change it like this to make 2d array?
NO NO.

int main()
{
    int array[140][2] = {0};
    ifstream myfile("filename.txt");
    int i = 0;
    while( myfile >> array[i][0] >> array[i][1] )
           i++;
    myfile.close();
}

Why no output is appear?

#include <iostream>   // std::cout
#include <fstream>
#include <iomanip>
#include <string>    // std::string
#include <vector>    // std::vector<>
#include <algorithm> //std::for each()
#include <iostream>   // std::cout
#include <fstream>
#include <iomanip>
#include <string>    // std::string
#include <vector>    // std::vector<>
#include <algorithm> //std::for ea


using namespace std; // import "std" namespace into global namespace

int main()
{
    int array[140][2] = {0};
    ifstream myfile("STA83SOLUTION.txt");
    int i;
    while( myfile >> array[i][0] >> array[i][1] )
           i++;

{
	ofstream myfile;
	myfile.open("411.txt");
    myfile.close();
}

	if (myfile.is_open())
	{
		for (int i = 0; i < array[i][0]; i++) 
		{
			for (int j = 0; j<array[i][0]; j++) 
			{
		cout<<"\n"<<i+1<<":"<<" "<< array[i][0]<<"\t"; // output list of exam codes for this student
			}
		}
	}
}

F:\508.cpp(35) : warning C4508: 'main' : function should return a value; 'void' return type assumed

>>Why no output is appear
Think about what you are doing. Look at lines 30 and 35. Can you guess the value of array[0] on the first iteration of that loop ? Hint: see line 19.

F:\508.cpp(35) : warning C4508: 'main' : function should return a value; 'void' return type assumed

add return 0; at the end of main()

So I must chane [0] to [140][2]?

int array[140][2] = {0};
    ifstream myfile("STA83SOLUTION.txt");
    int i;
    while( myfile >> array[i][0] >> array[i][1] )
           i++;

{
	ofstream myfile;
	myfile.open("508.txt");
    myfile.close();
}

	if (myfile.is_open())
	{
		for (int i = 0; i < array[140][2]; i++) 
		{
			for (int j = 0; j<array[140][2]; j++) 
			{
		cout<<"\n"<<i+1<<":"<<" "<< array[i][2]<<"\t"; // output list of exam codes for this student
			}
		}
	}return 0;

>>So I must chane [0] to [140][2]?

No. Just forget it. You have no clue what you are doing.

so..I must continue this programming or continue the old one?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.