Jump to content

C++ question: Debug Code

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Corum

Corum

    Newbie

  • Members
  • Pip
  • 3 posts
Hello! I need some help (err...urgent) on debugging this code. Tha main question is, Is the principle of it correct? beside the typo's. Just to know if I am on the right track.
Anyway, it's a program that add the value of two .txt files and creates the answer in a third.
I would really thank some help =)

BTW, C++

#include <stdio.h>

#include <stdlib.h>


const int DIM=10;

int read(char*nome,int v[],int n);

int read(char*nome,int w[],int n);

void ecra(int v [],int n);



int main (void)

{

int v[DIM];

if(read("fich1.txt",v,DIM)==0);

	{

printf("Erro na leitura!");

exit(0);

	}

return (1);

}


int read(char*nome,int v[],int n)

	{ 

FILE*fp;

int i;


fp=fopen(nome,"r");

if(fp==NULL)

		{

return 0;

		}

for(i=0;i<n;i++)


	{

fscanf(fp,"_%d",&v[i]);

	}

return 1;

	}

}


void ecra(int v [],int n)

{

int i;

while(i<n)

{

	printf("%d\n",v[i]);

	i++;

}


// 2nd


int main (void)

{

int w[DIM];

if(read("fich2.txt",w,DIM)==0);

	{

printf("Erro na leitura!");

exit(0);

	}

return (1);

}


int read(char*nome,int w[],int n)

	{ 

FILE*fp;

int i;


fp=fopen(nome,"r");

if(fp==NULL)

		{

return 0;

		}

for(i=0;i<n;i++)


	{

fscanf(fp,"_%d",&w[i]);

	}

return 1;

	}

}


void ecra(int w [],int n)

{

int i;

while(i<n)

{

	printf("%d\n",w[i]);

	i++;

}



//sum



void soma(int z,int v[], int w[])

for(i = 0; i < 9; i++);

	{

		total[i] = v[i] + w[i];

	}

	


	for(i = 0; i < 9; i++);

	{

		printf("%d ,",total[i]);

	}



	fp = fopen("resultado.txt","z");

	

	if(fp == NULL);

	{

		printf("Erro ao abrir resultado!\n");

	}

	else{

		for(i = 0; i < 9; i++);

		{

			fprintf(fp,"%d;",total[i]);

			

		}

		printf("Ficheiro criado com exito. \n");

	}


#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
By the way, this is C not C++. Also, next time please use code tags ( # button ).

Code seems solid (on a quick glance), except that in ecra() function you don't initialize i.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users