This Program SEEMS Right..... :\

B

blackneos940

Guest
Okay..... I feel like pulling my hair out..... :( I tweaked this Program, only to come across MORE errors....... D: It's the one where I showed you guys a snippet, and you helped me..... But this Program is just for PRACTICE, and I can't even get THAT right..... X( GAH!!!..... D:< ..... Oh, wait, I should probably post the Code.... :3

Code:
/*The goal of this program is to test my skills in utilizing Switch Statements, coupled with initiating 2 Switch Statements*/

#include <stdio.h>

int main(void)

{
  int test;

  test = 1; /*Here is the FIRST Integer, being given a Value of "1"*/

  char *test_two[] = 'Fuzzy Mittens.'; /*And the first (and ONLY) Char, being given a String Value of "Fuzzy Mittens." We allow just ONE more Character than what is readily visible, since the Compiler adds an "\0" Termination Format Specifier to the end of the String.   :)*/

  switch (test)

  {
    case 1:

      printf ("Test 1.\n");

      printf ("Test 1a.\n");

    case 2:

      FILE *ptr; /*We're getting fancy here now. Nested within this first Switch Statement Case, of Switch (test), we declare a Pointer for a Variable, which is just called "ptr". The Asterisk Symbol is used as a Pointer to bind it to FILE.*/

      ptr = fopen ("test.txt", "w");

      fprintf ("This is a test.\n\"Hello\nWorld!!\"", ptr);

      fflush (ptr);

      fclose (ptr);

      ptr = fopen ("test.txt", "a+");

      fprintf ("Hello, \"World!!\"", ptr);

      fflush (ptr);

      fclose (ptr);

      break;

    case 3:

      printf ("Nothing??\n");

      printf ("0x3BFD1YA");

      printf ("\n");

    default:

      printf ("Tihs is teh defualt.\nPl0x...?\nU Jelly...?");

  }

  switch (test_two) /*This is the SECOND Switch Statement. It is called "test_2". This is what the Char Variable was called. :)*/

  {
    case "Clash"; /*I'm playing with Strings here, now. My hope is that Char (which Equals "Fuzzy Mittens.", by the way), will activate Case #2, and Print ""Clash with Team Magma...?? This is simply a reference to Pokemon Versions Ruby, Sapphire, and Emerald, and the remakes, Omega Ruby, and Alpha Sapphire */

      printf ("Another test.\n");

      printf ("0x3YBY2K");

    case "Fuzzy Mittens.":

      printf ("Clash with Team Magma...??\n");

    default:

      printf ("Tihs is ALSO teh defualt...\n");

  }

  return 0;

}

/*And we're DONE! :D So, what'dya think...?   :)*/

Now, for those of you who saw this GCC Error Message before:
Code:
Switch_Palace.c: In function ‘main’:
Switch_Palace.c:12:22: warning: character constant too long for its type [enabled by default]
   char *test_two[] = 'Fuzzy Mittens.'; /*And the first (and ONLY) Char, being given a String Value of "Fuzzy Mittens." We allow just ONE more Character than what is readily visible, since the Compiler adds an "\0" Termination Format Specifier to the end of the String.   :)*/
                      ^
Switch_Palace.c:12:3: error: invalid initializer
   char *test_two[] = 'Fuzzy Mittens.'; /*And the first (and ONLY) Char, being given a String Value of "Fuzzy Mittens." We allow just ONE more Character than what is readily visible, since the Compiler adds an "\0" Termination Format Specifier to the end of the String.   :)*/
   ^
Switch_Palace.c:25:7: error: a label can only be part of a statement and a declaration is not a statement
       FILE *ptr; /*We're getting fancy here now. Nested within this first Switch Statement Case, of Switch (test), we declare a Pointer for a Variable, which is just called "ptr". The Asterisk Symbol is used as a Pointer to bind it to FILE.*/
       ^
Switch_Palace.c:29:7: warning: passing argument 1 of ‘fprintf’ from incompatible pointer type [enabled by default]
       fprintf ("This is a test.\n\"Hello\nWorld!!\"", ptr);
       ^
In file included from Switch_Palace.c:3:0:
/usr/include/stdio.h:356:12: note: expected ‘struct FILE * __restrict__’ but argument is of type ‘char *’
extern int fprintf (FILE *__restrict __stream,
            ^
Switch_Palace.c:29:7: warning: passing argument 2 of ‘fprintf’ from incompatible pointer type [enabled by default]
       fprintf ("This is a test.\n\"Hello\nWorld!!\"", ptr);
       ^
In file included from Switch_Palace.c:3:0:
/usr/include/stdio.h:356:12: note: expected ‘const char * __restrict__’ but argument is of type ‘struct FILE *’
extern int fprintf (FILE *__restrict __stream,
            ^
Switch_Palace.c:29:7: warning: format not a string literal and no format arguments [-Wformat-security]
       fprintf ("This is a test.\n\"Hello\nWorld!!\"", ptr);
       ^
Switch_Palace.c:37:7: warning: passing argument 1 of ‘fprintf’ from incompatible pointer type [enabled by default]
       fprintf ("Hello, \"World!!\"", ptr);
       ^
In file included from Switch_Palace.c:3:0:
/usr/include/stdio.h:356:12: note: expected ‘struct FILE * __restrict__’ but argument is of type ‘char *’
extern int fprintf (FILE *__restrict __stream,
            ^
Switch_Palace.c:37:7: warning: passing argument 2 of ‘fprintf’ from incompatible pointer type [enabled by default]
       fprintf ("Hello, \"World!!\"", ptr);
       ^
In file included from Switch_Palace.c:3:0:
/usr/include/stdio.h:356:12: note: expected ‘const char * __restrict__’ but argument is of type ‘struct FILE *’
extern int fprintf (FILE *__restrict __stream,
            ^
Switch_Palace.c:37:7: warning: format not a string literal and no format arguments [-Wformat-security]
       fprintf ("Hello, \"World!!\"", ptr);
       ^
Switch_Palace.c:59:11: error: switch quantity not an integer
   switch (test_two) /*This is the SECOND Switch Statement. It is called "test_2". This is what the Char Variable was called. :)*/
           ^
Switch_Palace.c:62:17: error: expected ‘:’ or ‘...’ before ‘;’ token
     case "Clash"; /*I'm playing with Strings here, now. My hope is that Char (which Equals "Fuzzy Mittens.", by the way), will activate Case #2, and Print ""Clash with Team Magma...?? This is simply a reference to Pokemon Versions Ruby, Sapphire, and Emerald, and the remakes, Omega Ruby, and Alpha Sapphire */
                 ^
Switch_Palace.c:68:5: error: case label does not reduce to an integer constant
     case "Fuzzy Mittens.":

then you don't need to look at that, as only the first message about the Char-thingy changed, since I changed it..... The ONLY thing I recognize is where it says that things like "case 'Fuzzy Mittens':" is not the proper thing to do..... I'm sorry, guys, really, I am....... :'( It's just that I'm alone, here..... :( I've got to make sense of this, all by myself, and, if I can articulate my words clearly enough, maybe, just maybe, someone will actually know what I'm trying to convey, and be able to guide me (and let's be honest: I suck at conveying my thoughts..... :))..... *Sigh* What I would only give to be a great Programmer..... :'( Well, back to Nano, to try to sort this living hell out.......
 


Sorry, I can't help you mate, but I'm rooting for you!:D:D I've found that when I make a mistake in a program, I just walk away from it for a couple days, and when I come back, the solution beats me over the head!!:oops:
 
Sorry, I can't help you mate, but I'm rooting for you!:D:D I've found that when I make a mistake in a program, I just walk away from it for a couple days, and when I come back, the solution beats me over the head!!:oops:

No worries, buddy....... :) I'll figure it out..... Hopefully. :( Anywho, until someone can help, I've decided to go bak through my Materials on C (particularly the ones about Char.....) :3
 
Sorry, I can't help you mate, but I'm rooting for you!:D:D I've found that when I make a mistake in a program, I just walk away from it for a couple days, and when I come back, the solution beats me over the head!!:oops:
Also, I accidentally mispelled "back", but I left it there as a little reference to some Extension in Linux....... :3 :D
 
Sorry this took so long. I was working on something else:

Several problems
Code:
char *test_two[] = 'Fuzzy Mittens.';

// Should be:

char test_two[] = "Fuzzy Mittens.";
Single quotes for a single character, double quotes for a string of characters. Also, this is an array of characters, you declared an array of pointers to char.

In "case 2:", "FILE *ptr;" should be defined at the top of the function main(), not inside of a case in a switch statement.
Code:
fprintf ("This is a test.\n\"Hello\nWorld!!\"", ptr);

// Should be:

fprintf (ptr, "This is a test.\n\"Hello\nWorld!!\"");
If you are closing a file, there is no need to "fflush(ptr);" first. The closing of the file will do this.

The same of the second fprintf(), and fflush().

Also, when you open a file, you should test if the file opened correctly, before attempting to read from the file, or write to the file.

Your second switch and case are illegal. The parameter to a switch statement, and the cases must be an integer constant, not a string.

Start with these corrections, and test again.
 

Members online


Top