Quantcast

Jump to content

» «
Photo

Coding probelm.

3 replies to this topic
Luke2004
  • Luke2004

    You wanna leave? k. gtfo.

  • BUSTED!
  • Joined: 10 Nov 2004

#1

Posted 08 May 2005 - 08:10 PM

Well, I've made this basic code for a little text editor the only problem is when I run it, it runs, cmd.exe not text.exe (which is what I've named it.) I was wondering also whether it should be an %e or %s anyway here's the code:
CODE

\* TEXT EDITOR BY Luke and Ricky. *\
#include <stdio.h>

int main()
{
   int text;
   int choice;
   char ofilename[] = "C:/text.txt";
   FILE *ofp;
   printf("-=> TEXT EDITOR <=-\n");
   printf("Please enter all the text you want.\n");
   scanf("%e",&text);
   printf("Please enter 1 for saving and anything else to clear your work.\n");
   scanf("%d",&choice);
   switch (choice) {
   case 1:
   ofp = fopen(ofilename,"w");
   fprintf(ofp,"%e",text);
   fclose(ofp);
   case 2:
   printf("You found it, the hidden surprise.\n");
   default:
   system("PAUSE");
   break;
    }
    return 0;
    }
 


It'll compile without any problems, but it won't run. Please help me and tell me where I went wrong so I don't make the same mistake again. smile.gif

This is the part where I'm debating about:

CODE
 
scanf("%e",&text);
   printf("Please enter 1 for saving and anything else to clear your work.\n");
   scanf("%d",&choice);
   switch (choice) {
   case 1:
   ofp = fopen(ofilename,"w");
   fprintf(ofp,"%e",text);

Should it be an %e or %s.
I've found a description for them but can't decide:

Specifier Argument Type
%d int
%f float or double
%e float or double, output in scientific notation.
%c character
%s character string (char *)

--------------------------------------------------------------------------------------------
Any help will be greatly appreciated.

Svip
  • Svip

    I eat babies

  • The Connection
  • Joined: 12 Nov 2001
  • None

#2

Posted 08 May 2005 - 09:16 PM

This should work:

CODE
\* TEXT EDITOR BY Luke and Ricky. *\
#include <stdio.h>

int main()
{
  char text;
  int choice;
  char ofilename[] = "C:/text.txt";
  FILE *ofp;
  printf("-=> TEXT EDITOR <=-\n");
  printf("Please enter all the text you want.\n");
  scanf("%s",&text);
  printf("Please enter 1 for saving and anything else to clear your work.\n");
  scanf("%d",&choice);
  switch (choice) {
  case 1:
  ofp = fopen(ofilename,"w");
  fprintf(ofp,"%e",text);
  fclose(ofp);
  case 2:
  printf("You found it, the hidden surprise.\n");
  default:
  system("PAUSE");
  break;
  }
 return 0;
}


Your problem?

The 'text' was declared as a number, and not as a string, you need to declared it like so;

CODE
char <variable> [[length]]

The length is optical, that's why there is [] around it.

Secondly, you wont need '%e', but '%s'.

Try it now.

segosa
  • segosa

    Ghetto Star

  • BUSTED!
  • Joined: 15 May 2002

#3

Posted 08 May 2005 - 09:43 PM Edited by Segosa, 08 May 2005 - 09:43 PM.

QUOTE (Svip @ May 8 2005, 23:16)
This should work:

CODE
\* TEXT EDITOR BY Luke and Ricky. *\
#include <stdio.h>

int main()
{
  char text;
  int choice;
  char ofilename[] = "C:/text.txt";
  FILE *ofp;
  printf("-=> TEXT EDITOR <=-\n");
  printf("Please enter all the text you want.\n");
  scanf("%s",&text);
  printf("Please enter 1 for saving and anything else to clear your work.\n");
  scanf("%d",&choice);
  switch (choice) {
  case 1:
  ofp = fopen(ofilename,"w");
  fprintf(ofp,"%e",text);
  fclose(ofp);
  case 2:
  printf("You found it, the hidden surprise.\n");
  default:
  system("PAUSE");
  break;
  }
 return 0;
}


Your problem?

The 'text' was declared as a number, and not as a string, you need to declared it like so;

CODE
char <variable> [[length]]

The length is optical, that's why there is [] around it.

Secondly, you wont need '%e', but '%s'.

Try it now.

No Svip.

CODE

char text;


is one character. If he wants to store more, he needs to define enough space for it.

CODE

char text[1024];


CODE

  scanf("%s",&text);


The name of a char* on its own is its address, & is only needed for ints which are not pointers by default.

And scanf does not work for strings. Use gets().

CODE

gets(text);


CODE

  fprintf(ofp,"%e",text);


to

CODE

  fprintf(ofp,"%s",text);


You also need break; statements after each case, else it continues to the next one.

Luke2004
  • Luke2004

    You wanna leave? k. gtfo.

  • BUSTED!
  • Joined: 10 Nov 2004

#4

Posted 09 May 2005 - 03:57 PM Edited by Luke2004, 09 May 2005 - 07:28 PM.

QUOTE (Segosa @ May 8 2005, 22:43)
No Svip.

CODE

char text;


is one character. If he wants to store more, he needs to define enough space for it.

CODE

char text[1024];


CODE

  scanf("%s",&text);


The name of a char* on its own is its address, & is only needed for ints which are not pointers by default.

And scanf does not work for strings. Use gets().

CODE

gets(text);


CODE

  fprintf(ofp,"%e",text);


to

CODE

  fprintf(ofp,"%s",text);


You also need break; statements after each case, else it continues to the next one.

Thanks Segosa. It worked, fabulous. smile.gif cookie.gif




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users