Can you please help !!

Here, you see that

const int size=25;

Actually, How can i use infinite number like (n).
I used "n" like bellow

int n;
const int size = n;

however it only takes 8 values.

My txt file can contained different value(n) evrytime, so, i dont want to keep the const int size fixed. please help me.

If I keep const int value, i can't use for more/less number. To da this always i have to change const int size value.

the software given bellow.

#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iomanip>
#include <windows.h>
using namespace std;
//void calculateAverage();
//int calculateGrade();
//void updateFrequency();
int main()
{
const int size=25;
int debug=size;
double test1[size], test2[size];
ifstream infile;
for(int i=0;i<size;i++)
{
test1[i]=0;
test2[i]=0;
}
cout<<"Reading from file ''input.txt''..."<<endl;
infile.open("input.txt");
while(size==debug)
{
for(int i=0;i<size;i++)
{
infile>>test1[i];
infile>>test2[i];
}
debug--;
}
for(int i=0;i<size;i++)
{

cout<<test1[i]<<" "<<test2[i]<<" "<<endl;
}
infile.close();
system("pause");
return 0;
}

Recommended Answers

All 8 Replies

First off, you've committed an unpardonable sin on DaniWeb. You used system("pause"); Using getch(); is not better. This is a crime punishable by excessive belittling, arrogant condescension, and ostricism by the veteran community members.
I don't make the rules, and I am not making this up.
(The fix is to compile in the IDE, then run from the command-line, or find something in the preferences to

Second, you're attacking the problem the wrong way. Where (n) is not a constant, you want dynamic arrays in one form or fashion. The not fun way is to create the array with a single data slot, then allocate a new one, copy, and delete the old one every single time you add a new number.
This is less than ideal.

The "right way" (read: oh so much easier), is to use the pre-bundled Standard Template Library data types, most notably vector.
Here's an example:

#include <vector> //Include the header for the vector data type
#include <iostream> //Accept input and give output.

using namespace std; //Using the std namespace for coding simplicity

int main(){
    vector<double> array;
    double num;
    int i=0,j;
    cin.clear(); //clear any error flags
    cout<<"Please enter as many numbers as you like followed by a non-number to quit."<<endl; //So polite!

    //Accept an undefined number of double-precision floats.
    while(true){
        cin>>num;

        //Makes sure that the input type was the right data type.
        //(i.e. putting a string of letters into a number variable would be bad.)
        if(!cin.good()) break; 

        array.push_back(num); //Back of the line for you, mister!
        i++; //And one more please.
    }

    //Print them back out.
    cout<<"The numbers you entered were:"<<endl; 
    for(j=0;j<i;j++){
        cout<<array[j]<<endl;
    }

    //Clean up!
    cin.clear(); //clear any error flags
    array.clear(); //Clean up and free up!
    return 0;
}

Have a look here.
http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html

Note that you will have to rewrite a lot of your code from scratch, but it'll be easier with a clear path in mind instead of fumbling in the dark.

sorry sir.
Thank you.
Actually, i don't know programing, even i dont know that using system ("pause"); is a sin. This is right that, you teach well but i didn't get it as i don't know how to use your idea to my program. Anyway, i am going to mark it solved and errasing syste("pause"); as well. Thank you !!!

Dear Sir, before I leave, i want to ask another help and this is as follows, in your program i only can input data after running it. but, i have a txt file having 2 column( where there are 1000-2000 data in each) , described as test1[size],test2[size] in my post. Can you please say somthing about how to input this txt file?

Ignore the bit about system("pause"); . I was being sarcastic. That's just something veteran coders use to humiliate junior coders. Using it IS a bad habit, but one that most of us (myself included) start out with.

As to your question, instead of double test1[size], test2[size]; use vector<double> test1, test2;
Next, read the numbers from infile into a temporary variable (make sure to declare it first).
Then, use the push_back member function to add the number into test1 or test2.
(A member function is a class function whose actions are -generally- specific to the object calling it).
You check the End of File flag on infile to see if you've run out of data.
I'll post an example shortly. First I need to rotate my laundry.

OK... strange... it didn't post this morning. Here's the repost:

#include <vector> //Include the header for the vector data type
#include <iostream> //Accept input and give output.
#include<fstream>

using namespace std; //Using the std namespace for coding simplicity

int main(){
    vector<double> test1,test2;
    double num;
    int i=0,j;
    ifstream infile;
    infile.open("input.txt");
    while(true){
        //First column
        infile>>num;
        if(!infile.good()) break; //If it's not good break.
        if(infile.eof()) break; //If it's the end of the file break.
        test1.push_back(num); //Back of the line for you, mister!

        //Second column
        infile>>num;
        if(!infile.good()) break; //If it's not good break.
        if(infile.eof()) break; //If it's the end of the file break.
        test2.push_back(num); //Back of the line for you, mister!

        i++; //And one more row please.
    }

    //Print them back out switched just for variety.
    for(j=0;j<i;j++){
        cout<<test2[j]<<"\t"<<test1[j]<<endl; //Notice I'm printing test2 first, then test1.
    }

    test1.clear(); //Clean up and free up!
    test2.clear();
    infile.close();
    return 0;
}

Here's the data set I used to test it:

0.5 -1.0
1.5 -2.0
2.5 -3.0
3.5 -4.0
4.5 -5.0

And here's what it prints out:

-1 0.5
-2 1.5
-3 2.5
-4 3.5
-5 4.5

O M G !!! Thank you sir for your kind reply.how can I say you that how much i feel happy !!! thank you sir....

Better while loop, avoiding breaks... :-)

    bool done = false;
    while (!done)
    {
        //First column
        infile>>num;
        if(!infile.good() || infile.eof())
        {
            done = true;
        }
        else
        {
            test1.push_back(num); //Back of the line for you, mister!
            //Second column
            infile>>num;
            if(!infile.good() || infile.eof())
            {
                done = true;
            }
            else
            {
                test2.push_back(num); //Back of the line for you, mister!
                i++; //And one more row please.
            }
        }
    }

Thank you one more !!!

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.