PDA

View Full Version : need help with a text file


cv87cv
12-13-2006, 10:11 AM
I am trying to answer a question with regards to entering a set of numbers and text, and then I have got to search for a number and the text with it.

This is the code that I have written for inputting the numbers:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;

int main()
{
char name[20];
int mark;
int x;
FILE *f;
f = fopen("black.txt","w");
for(x=1; x<=5; x++)
{
printf("Enter a name \n");
gets(name);
printf("Enter a mark \n");
scanf("%d",&mark);
fprintf(f,"%s %d\n",name, mark);
}

fclose(f);
return 0;
}

it will not input the number and mark correctly together

the other part of the code to display a certain number and text is :


#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;

int main()
{
char name[20];
int number;
int x;
FILE *f;
f = fopen("black.txt","w");
while ( fscanf(f,”%s”,name )
{
if (number=10)
{
printf(“%s \n”,name);
}
}
fclose(f);
return 0;
}

this part is not working properly either and I am unable to spot what is wrong with it. i think because i've been looking at it for a while i just cant see the problems

so if someone can help me that would be great!

Mink
12-13-2006, 07:28 PM
Do you have 2 main methods in your program?


What exactly is your problem? Is it not compiling? If so what is the error you are getting.


Is this a runtime error? if so what is the error you are getting?

Is it a logic error? If so what are you getting as output that you shouldn't be?


I'm sorry, but I don't understand what your problem is.