Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Array program problems
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
JR




PostPosted: Sun Nov 07, 2004 6:22 pm   Post subject: Array program problems

Ok heres my program


code:


//Alex Roizin
//A7Part 1
//October 29th 2004
//The array program, this will do some operations with arrays

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
int check=0;
ifstream fin("data7.txt");
void load(int age[]);
void arg();
void lrg();
void sml();
void acen();
void decn();
void out();



int  menu(int y)
{
int choice;
cout<<"////////////////////////////////////////////\n";
cout<<"////"<<"1. Load the array"<<"\t\t\t////\n";
cout<<"////"<<"2. Array Avarage"<<"\t\t\t////\n";
cout<<"////"<<"3. Find largest element"<<"\t\t////\n";
cout<<"////"<<"4. Find Smallest element"<<"\t\t////\n";
cout<<"////"<<"5. Arrange in Acending order"<<"\t////\n";
cout<<"////"<<"6. Arrange in Decending order"<<"\t////\n";
cout<<"////"<<"7. Output the array"<<"\t\t\t////\n";
cout<<"////"<<"8. Exit"<<"\t\t\t\t////\n";
cout<<"////////////////////////////////////////////\n";

cin>>choice;
if (choice == 1)
{
        check=1;
}
if (y!=0)
{
y=choice;
return (choice);
}
}



void load(int age[])
{


if (!fin)
{
cout<<"No file data7.txt found";
}
else
{
for (int i=0;i<25;i++)
{
int age[25];
fin>>age[i];
}
cout<<"Array has been loaded\n";
fin.close();

}
}

void out()
{

if (check==1)
{
for (int i=0;i<25;i++)
{
int age[25];
cout<<age[i]<<endl;
}
}
}


void arg()
{

}

void lrg()
{

}

void sml()
{

}

void acen()
{

}

void decn()
{

}



int main()
{

int age[25]={0};
int choice;
int y;
menu(0);
choice=menu(y);


switch (choice)
{
case 1: load(age);
break;

case 2: out();
break;

default: cout<<"test";
break;
}
cin.get();
return 0;

}




now its not done yet cause first i want to make sure hte output and input works properly. after that everything is easy.

now what i'm trying to do is first run a menu, then if the user has to load the array, if its loaded the numbers will be xxxxxxxx 25 numbers.

then when i press out it should output the array, if it has been loaded then it will be the numbers if not 0. how do i make it work? oh and i want the menu to return after each function is completed.
Sponsor
Sponsor
Sponsor
sponsor
JR




PostPosted: Sun Nov 07, 2004 8:13 pm   Post subject: (No subject)

ok i fixied a little thx to dan but i still get some errors...

code:


//Alex Roizin
//A7Part 1
//October 29th 2004
//The array program, this will do some operations with arrays

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
int check=0;
ifstream fin("data7");
void load(int age[24]);
void arg();
void lrg();
void sml();
void acen();
void decn();
void out();



int  menu(int y)
{
int choice;
cout<<"////////////////////////////////////////////\n";
cout<<"////"<<"1. Load the array"<<"\t\t\t////\n";
cout<<"////"<<"2. Array Avarage"<<"\t\t\t////\n";
cout<<"////"<<"3. Find largest element"<<"\t\t////\n";
cout<<"////"<<"4. Find Smallest element"<<"\t\t////\n";
cout<<"////"<<"5. Arrange in Acending order"<<"\t////\n";
cout<<"////"<<"6. Arrange in Decending order"<<"\t////\n";
cout<<"////"<<"7. Output the array"<<"\t\t\t////\n";
cout<<"////"<<"8. Exit"<<"\t\t\t\t////\n";
cout<<"////////////////////////////////////////////\n";

cin>>choice;
if (choice == 1)
{
   check=1;
}
if (y!=0)
{
y=choice;
return (choice);
}
}

void load(int age[24])
{
if (!fin)
{
cout<<"No file data7.txt found\n";
system("PAUSE");
exit(1);
}
else
{
for (int i=1;i<25;i++)
{
fin>>age[i];
}
cout<<"Array has been loaded\n";
fin.close();
}
}


void out(int age[24])
{
if (check==1)
{
for (int i=0;i<25;i++)
{
cout<<age[i]<<endl;
}
}
}





void arg()
{

}

void lrg()
{

}

void sml()
{

}

void acen()
{

}

void decn()
{

}



int main()
{

int age[25]={0};
int choice;
int y;
choice=menu(y);


switch (choice)
{
case 1: load(age);
break;

case 2: out(age);
break;

default: cout<<"test";
break;
}
choice=menu(y) ;


switch (choice)
{
case 1: load(age);
break;

case 2: out(age);
break;

default: cout<<"test";
break;
}
cin.get();
cin.get();
return 0;

}




the main error here basically that it doesnt load the file!!!! i dont know why but it gives me the error file not found... dont know why i have the file and i have the stuff in it and everythings in the same folder, but it gives me the error. I'm using Dev C++ can someone else check that out?
wtd




PostPosted: Mon Nov 08, 2004 12:27 am   Post subject: (No subject)

There a a lot of things you need to change about this program. Read the code I'm posting and if you have any questions, ask.

code:
// "iostream.h" and "fstream.h" are deprecated.
#include <iostream>
#include <fstream>
#include <stdlib.h>

// No global variables!
// int check=0;
// ifstream fin("data7.txt");

// You forward declare your functions, but then put your main function
// at the end of your source file.
// Plus, these functions are horrifically badly named.  Use good
// variable and function names and it'll be easier to debug.
int menu(bool& check);
void load(int age[], int length, std::ifstream& inputFile);
void arg();
void lrg();
void sml();
void acen();
void decn();
void out(int age[], int length, bool check);

int main()
{
        int length(25);
        int age[length];
        bool check(false);
        int choice(menu(check));
        std::ifstream inputFile("data7.txt");

        switch (choice)
        {
                case 1:
                        load(age, length, inputFile);
                        break;

                case 2:
                        out(age, length, check);
                        break;

                default:
                        std::cout << "test" << std::endl;
                        break;
        }
       
        std::cin.get();
        return 0;

}

int menu(bool& check)
{
        // Always give variables an initial value
        int choice(0);
       
        // If you're not "using namespace std", you have to qualify the cout name
        // and anything else in that namespace.
        // "\n" may not be portable.  Use std::endl.
        // There's no need to make this a series of multiple statements.
        std::cout << "////////////////////////////////////////////" << std::endl
                  << "////1. Load the array                   ////" << std::endl
                  << "////2. Array Avarage                    ////" << std::endl
                  << "////3. Find largest element             ////" << std::endl
                  << "////4. Find Smallest element            ////" << std::endl
                  << "////5. Arrange in Acending order        ////" << std::endl
                  << "////6. Arrange in Decending order       ////" << std::endl
                  << "////7. Output the array                 ////" << std::endl
                  << "////8. Exit                             ////" << std::endl
                  << "////////////////////////////////////////////" << std::endl;

        std::cin >> choice;
       
        if (choice == 1)
        {
                 check = true;
        }
       
        return choice;
}



void load(int age[], int length, std::ifstream& inputFile)
{
        if (!inputFile)
        {
                // Don't send errors to standard output.
                // Send them to standard error.
                std::cerr << "No file data7.txt found" << std::endl;
        }
        else
        {
                for (int i = 0; i < length; i++)
                {
                        inputFile >> age[i];
                }
               
                std::cout << "Array has been loaded." << std::endl;
               
                inputFile.close();
        }
}

void out(int age[], int length, bool check)
{
        if (check)
        {
                for (int i = 0; i < length; i++)
                {
                        // This just creates a new array.
                        // int age[25];
                        std::cout << age[i] << std::endl;
                }
        }
}


void arg()
{

}

void lrg()
{

}

void sml()
{

}

void acen()
{

}

void decn()
{

}



Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: