I Made A Shopping List Generator!!..... ^^

B

blackneos940

Guest
So here's the Code: :3

Code:
#include <stdio.h>

int main(void)

{
  printf ("Welcome to the Happy Shopper Program!\nType the items on your Shopping List, and then press Enter!\n");

  char *items = "";

  scanf ("%s", items);

  FILE *ptr;

  ptr = fopen("/home/adam/Documents/Shopping_Lists/Shopping_List.txt", "w");

  fprintf (ptr, "%s", items);

  fflush(ptr);

  fclose(ptr);

  return 0;

}

Can Mr. Penguin and Bethlehem guess it.....? That's right: It results in a Segmentation Fault....... :< I don't want the Gubmint exploiting that flaw..... How the fsck do I fix that.....? D: Yes, I had such strong Coffee that I grew Chest Hair on my Chest Hair....... :> Thanks for any help, you guys....... :D
 


Okay..... I debugged my own Code!!..... :D It turns out that those Quotes caused the Seg Fault!...... :3 Now it Prints...... Only ONE Item..... Hmmm..... :\

Here's the updated Code:

Code:
#include <stdio.h>

int main(void)

{
  printf ("Welcome to the Happy Shopper Program!\nType the items on your Shopping List, and then press Enter!\n"); //Display welcome message

  char *items;

  scanf ("%s", items); //Gets Grocery Items input

  FILE *ptr;

  ptr = fopen("/home/username_expunged/Documents/Shopping_Lists/Shopping_List.txt", "w"); //Here, we open the File for Writing

  fprintf (ptr, "%s", items); //We Write to the File here

  fflush(ptr); //Here, we flush the Data from Memory...

  fclose(ptr); //Aaaaannnd we close :)

  return 0;

}

Now then..... How do I Write MULTIPLE Items to the Grocery List.....? A List of some sort......? :>
 
Last edited:
Not debugged enough! ;^)
Code:
char *items;
This creates a pointer to a char, but no memory has been allocated for the character array! You don't know where in memory the pointer is pointing to!

For multiple strings, you would need something like:
Code:
char items[50][100];
Or an array of pointers and allocate the char arrays by calling malloc() for each string.
To get multiple "Items" you would need a loop and fgets() for each item, and another loop to write them to the file.
As for:
Code:
scanf ("%s", items); //Gets Grocery Items input
If I entered: "milk eggs, bread sugar..."
Your scanf() will only enter "milk", not the whole string.
Code:
fflush(ptr); //Here, we flush the Data from Memory...
fclose(ptr); //Aaaaannnd we close :)
You don't need to fflush() the file before closing. The file is automatically flushed when closing the file.

I could code this for you but then it would be my code not yours! ;^)

What you really need is a thorough understanding of arrays, pointers, character strings, etc...

You also need to turn up the warning levels for the compiler.

For gcc, try:
Code:
gcc -Wall -Wextra -pedantic ... file.c
 
Not debugged enough! ;^)
Code:
char *items;
This creates a pointer to a char, but no memory has been allocated for the character array! You don't know where in memory the pointer is pointing to!

For multiple strings, you would need something like:
Code:
char items[50][100];
Or an array of pointers and allocate the char arrays by calling malloc() for each string.
To get multiple "Items" you would need a loop and fgets() for each item, and another loop to write them to the file.
As for:
Code:
scanf ("%s", items); //Gets Grocery Items input
If I entered: "milk eggs, bread sugar..."
Your scanf() will only enter "milk", not the whole string.
Code:
fflush(ptr); //Here, we flush the Data from Memory...
fclose(ptr); //Aaaaannnd we close :)
You don't need to fflush() the file before closing. The file is automatically flushed when closing the file.

I could code this for you but then it would be my code not yours! ;^)

What you really need is a thorough understanding of arrays, pointers, character strings, etc...

You also need to turn up the warning levels for the compiler.

For gcc, try:
Code:
gcc -Wall -Wextra -pedantic ... file.c

Thanks for the advice, good sir..... :3 I know what malloc stands for, but how do I make an Array for char.....? :< Also, I know I need to try and do this myself..... :) If you do nothing but hold my hand, then I'll NEVER learn....... :3 But I still need SOME hand-holding, 'cuz..... Well, some things don't make sense to me..... But I'll look into adding those Loops, and let you know with some updated Code, ok.....? :)
 
Not debugged enough! ;^)
Code:
char *items;
This creates a pointer to a char, but no memory has been allocated for the character array! You don't know where in memory the pointer is pointing to!

For multiple strings, you would need something like:
Code:
char items[50][100];
Or an array of pointers and allocate the char arrays by calling malloc() for each string.
To get multiple "Items" you would need a loop and fgets() for each item, and another loop to write them to the file.
As for:
Code:
scanf ("%s", items); //Gets Grocery Items input
If I entered: "milk eggs, bread sugar..."
Your scanf() will only enter "milk", not the whole string.
Code:
fflush(ptr); //Here, we flush the Data from Memory...
fclose(ptr); //Aaaaannnd we close :)
You don't need to fflush() the file before closing. The file is automatically flushed when closing the file.

I could code this for you but then it would be my code not yours! ;^)

What you really need is a thorough understanding of arrays, pointers, character strings, etc...

You also need to turn up the warning levels for the compiler.

For gcc, try:
Code:
gcc -Wall -Wextra -pedantic ... file.c
SENPAI, I DID IT!!!..... ^^ I made it so it WRITES the File to a certain Directory, and I even used chdir()!!..... :D Man, I'm gettin' GOOD!!...... :3

Here's the Code: I know it's not quite what you suggested, but..... It DOES get ALL the Items though!..... ^^


Code:
#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

int main(void)

{
  printf ("Welcome to the Happy Shopper Program!\nType the items on your Shopping List, and then press Enter!\n"); //Display welcome
message

  char items[1000] = "";

  fgets (items, 1000, stdin); //Gets Grocery Items input

  char file_name[200] = "";

  FILE *ptr;

  chdir("/home/username_expunged/Documents/Shopping_Lists/");

  ptr = fopen("Shopping_List.txt", "w"); //Here, we open the File for Writing

  fprintf (ptr, "%s", items); //We Write to the File here

  fclose(ptr); //Aaaaannnd we close :)

  return 0;

}
 


Top