Hello.

I have created a simple program which generates 3 random numbers, puts them in a text file, reads them, and then does 3 small calculations, results are put in another text file.

Program is working correctly on my PC, however there is a catch. I need to submit it on the server of my University. Source file will then be compiled on the server, and answers of my program will be compared to the correct answers.

Problem is, I don't know what is the C++ version on the University server. All I know, it is running on Linux/GNU. I have tried submitting the file, and I keep receiving compilation errors.

#include <iostream>
#include <fstream>
#include <stdlib>

int main(){
			randomize();
         ofstream fails3;
			fails3.open("trissk.dat",ios::out);
   		int d=random(100)+1;
   		int e=random(100)+1;
   		int f=random(100)+1;
   		fails3<<d<<" ";
   		fails3<<e<<" ";
   		fails3<<f;
			fails3.close();


         int a,b,c;
         ifstream fails1;
         ofstream fails2;
         fails1.open("trissk.dat", ios::in);
         fails2.open("trissk.rez", ios::out);
         while(!fails1.eof())
         {
         			// mainigo seciba ir svariga...
         			fails1>>a;
               	               fails1>>b;
                           	fails1>>c;
         			if(!fails1.eof())
                             {
			cout<<a<<" "<<b<<" "<<c;
         			}

         }

        	if((a+b)>0||(b+c)>0||(c+a)>0){
        	fails2<<"VAR\n";}else{fails2<<"NEVAR\n";}

        	if((a+b)==0||(b+c)==0||(c+a)==0){
        	fails2<<"VAR\n";}else{fails2<<"NEVAR\n";}

			if((a+b)<0||(b+c)<0||(c+a)<0){
			fails2<<"VAR";}else{fails2<<"NEVAR";}

         fails2.close();
         fails1.close();
         return 0;
}

I am sure the problem has something to do with the bolded line and headers.

Compilation errors I receive after uploading the source file:

[B][I]../programs/34299.cpp:3:18: error: stdlib: No such file or directory[/I][/B]
../programs/34299.cpp: In function 'int main()':
../programs/34299.cpp:6: error: 'randomize' was not declared in this scope
../programs/34299.cpp:7: error: 'ofstream' was not declared in this scope
../programs/34299.cpp:7: error: expected `;' before 'fails3'
../programs/34299.cpp:8: error: 'fails3' was not declared in this scope
../programs/34299.cpp:8: error: 'ios' has not been declared
/usr/include/stdlib.h:327: error: too many arguments to function 'long int random()'
../programs/34299.cpp:9: error: at this point in file
/usr/include/stdlib.h:327: error: too many arguments to function 'long int random()'
../programs/34299.cpp:10: error: at this point in file
/usr/include/stdlib.h:327: error: too many arguments to function 'long int random()'
../programs/34299.cpp:11: error: at this point in file
../programs/34299.cpp:25: error: 'ifstream' was not declared in this scope
../programs/34299.cpp:25: error: expected `;' before 'fails1'
../programs/34299.cpp:26: error: expected `;' before 'fails2'
../programs/34299.cpp:27: error: 'fails1' was not declared in this scope
../programs/34299.cpp:27: error: 'ios' has not been declared
../programs/34299.cpp:28: error: 'fails2' was not declared in this scope
../programs/34299.cpp:28: error: 'ios' has not been declared
../programs/34299.cpp:37: error: 'cout' was not declared in this scope

Edited 6 Years Ago by Eternity[LK]: Extra information

Turbo C++ is your problem. There's a lot of nonstandard stuff in your program, like stdlib.h is #include <cstdlib>, and your randomize and random functions are non-standard. You also need to qualify all your library function calls (cout, ifstream,ostream, etc) with std:: or have using namespace std; (but not recommended). If you're on Windows, do yourself a favor and grab http://www.mingw.org/ the gcc (and g++) and you'll probably have a virtually identical compiler to your school's (and for free).

Edited 6 Years Ago by jonsca: n/a

If you are writing c++ code, I do not think there is a header file called

#include<stdlib>

. All i know are:

#include<cstdlib>

or

#include<stdlib.h>

Make the changes and let me know the result

@tkud
../programs/34299.cpp:3:18: error: stdlib: No such file or directory - line has disappeared.
Everything else is unchanged in the error report. I tried both of your suggestions:
#include<cstdlib>
#include<stdlib.h>


@jonsca
Borland C++ is the compiler used in the University. The task I have now is an exception, to let me know that there are other compilers, differences between them etc. I don't think installing another compiler is what I need at the moment. Thanks for your suggestion though. I guess I'll have to learn/use other compilers sooner or later.

Edited 6 Years Ago by Eternity[LK]: n/a

I have managed to make the code compatible with the C++ version on the server.
I had to use line "using namespace std;".

However, it appears my program is generating wrong answers 5 out of 10 times, according to the server.

There is a file trissk.dat on the server, which contains 3 integers in one row, for example:
113 -45 985

My program calculates if:
1.) sum of any 2 of the given integers is positive.
2.) sum of any 2 of the given integers is 0.
3.) sum of any 2 of the given integers is negative.

Results are put in file trissk.rez in this format:
YES
NO
NO

Any ideas on where the fault is. I personally can't think of any reason why the result generated by my program is wrong. I've tested it enough and re-checked results manually on my PC.

#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;

int main(){

         int a,b,c;
         ifstream file1;
         ofstream file2;
         file1.open("trissk.dat", ios::in);
         file2.open("trissk.rez", ios::out);
         while(!fails1.eof())
         {
         			file1>>a;
               	file1>>b;
               	file1>>c;
         }

        	[B]if((a+b)>0||(b+c)>0||(c+a)>0)
         {
        	file2<<"YES\n";}else{file2 <<"NO\n";
         }

        	if((a+b)==0||(b+c)==0||(c+a)==0)
         {
        	file2 <<"YES\n";}else{file2 <<"NO\n";
         }

			if((a+b)<0||(b+c)<0||(c+a)<0)
         {
			file2 <<"YES";}else{file2 <<"NO";
         }[/B]

         file2.close();
         file2.close();
         return 0;
}

I assume you have more than one row of data in the .dat file? If so, you need to wrap your if statements within your while so that once the data are read in and evaluated you can move on to the next line.
I changed the while loop a little, firstly because there are problems with using eof (do a quick search of threads here for it) but secondly because fails1 isn't defined anywhere in your program.

while(file1>>a)
         {
         	
               	file1>>b;
               	file1>>c;
         

        	if((a+b)>0||(b+c)>0||(c+a)>0)
                {
        	         file2<<"YES\n";}else{file2 <<"NO\n";
                 }

        	if((a+b)==0||(b+c)==0||(c+a)==0)
                {
        	        file2 <<"YES\n";}else{file2 <<"NO\n";
                }

	        if((a+b)<0||(b+c)<0||(c+a)<0)
                {
			file2 <<"YES";}else{file2 <<"NO";
                 }
} //end while

You also have file2.close twice.

I may have missed something but the answer you gave is correct for the numbers that you give -- sum of any 2 is positive, sum of any 2 is not zero, sum of any 2 is not negative.

Edited 6 Years Ago by jonsca: n/a

This article has been dead for over six months. Start a new discussion instead.