Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 25th, 2005, 10:05 PM   #1
brokenhope
Hobbyist Programmer
 
Join Date: Apr 2005
Posts: 126
Rep Power: 4 brokenhope is on a distinguished road
Making a Note Adder/ Editor/ Deleter/ Viewer thingy

Well the title explains it, im just making this to practice a few things ive learned. Im not sure how I will be able to do the editor or deleter, seems like it will be very hard, but ill deal with it when I come to it. Basicaly this stores everything to a .txt file and the program manipulates it.

First thing im having trouble with is making a while loop that never ends, since this will be the menu it will always be refrered back to when your done with the function...

#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>

// Variables
char choice1;
char choice2;
// Functions
void mainMenu();
void addNote();
void delNote();
void edtNote();
void viwNote();
using namespace std;

int main()
{
    mainMenu();
    system("PAUSE");
    return 0;
}

void mainMenu()
{
    while (choice1) {
    cout << "Main Menu/n1. Add Note/n2. Delete Note/n3. Edit Note/n4. View Notes\n\nOption: ";
    cin >> choice1;

         switch ( choice1 ) {
              case  '1':
              addNote();
              break;
              case '2':
              delNote();
              break;
              case '3':
              edtNote();
              break;
              case '4':
              viwNote();
         }
    }
}

void addNote() {
}
void delNote() {
}
void edtNote() {
}
void viwNote() {
}

I wanted to use gets() to get the input but it wouldnt work at all, so im stuck with cin. Anyways back to the loop I was trying to use while(choice1) { to work as long as choice1 exists but that doesnt work, it starts off as null, and sometimes it will be valueless, so im not even gonna try to make a piece of crap to get that to work, any ideas?
brokenhope is offline   Reply With Quote
Old Apr 25th, 2005, 10:27 PM   #2
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
the easiest way to do a loop like you want is to start it with this expression here

while(1)
{
}
that automatically evaluates to true and will loop forever. then whenever you want to exit the loop you add the keyword
break;
after any time you want to exit the loop, usually when the user wants to quit the program.
you also use the keyword
continue;
after any time you want to start back up at the beginning of the loop

try something like this:
#include <stdio.h>

int main()
{
  while(1)
  {
    char uchoice;

    puts("wanna quit?");
    scanf("%c", &uchoice);

    if (uchoice == 'y')
    {
      break;
    }

  }

return 0;

}

or in C++
#include <iostream>
using namespace std;

int main()
{
  while(1)
  {

    char uchoice;

    cout<<"wanna quit?"<<endl;
    cin>>uchoice;
    
    if (uchoice == 'y')
    {
      break;
    }
  }
return 0;
}
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Apr 25th, 2005, 10:40 PM   #3
brokenhope
Hobbyist Programmer
 
Join Date: Apr 2005
Posts: 126
Rep Power: 4 brokenhope is on a distinguished road
Ah thanks, I also updated the code a bit so its a little better... but the next thing I want to do is try to prevent overflows... since sometimes there will be more than 1000 characters inputed, so I dont know what to do in this case... what ive got is:

void addNote() {
cout << "Enter the text for the note: ";
char note[1000];
cin.ignore();
gets ( note );
cout << note;
}

Whenever I enter more than 1000 the application is forced to close, so any ways to prevent overflows? or automaticaly assign more memory? I think I read something about this in a dynamic memory tutorial but I didnt understand it much...
brokenhope is offline   Reply With Quote
Old Apr 25th, 2005, 11:06 PM   #4
Josef_Stalin
Programmer
 
Join Date: Apr 2005
Posts: 77
Rep Power: 4 Josef_Stalin is on a distinguished road
Here's an educated guess. Make a function similar to "gets" which checks to see if it is 1000 or smaller first before it inserts it.
Josef_Stalin is offline   Reply With Quote
Old Apr 26th, 2005, 1:53 AM   #5
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
this is one of those cases when you want to use malloc(C) or new(C++) to allocate your memory dynamically. this way, when you have LESS than 1000, you're not wasting any space. however, this gets more into pointers and the like so i would suggest doing what stalin said and test for your "fullness" condition BEFORE adding data to the array.

i'm a little drunk right now so "beer" with me...this might do it, but i'm not sure
if (note[999] != NULL)
    {
         cout<<"too much data!"<<endl;
         //<--now do something here to skip it, exit, whatever
    }
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.

Last edited by bl00dninja; Apr 26th, 2005 at 1:58 AM.
bl00dninja is offline   Reply With Quote
Old Apr 26th, 2005, 1:41 PM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Don't use gets - instead, use scanf or fgets, which both include ways to disregard more than x characters:
scanf("%999s", &note);
OR
fgets(note, 999, stdin);
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 26th, 2005, 2:05 PM   #7
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,473
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
since this is C++, I'd drop the char* type and use the string type... makes life much easier...

The infinite loop... I like do do this...

#define EVER ;;

for (EVER)
{
...code to exe ...
...break condition here...
}
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Apr 26th, 2005, 2:12 PM   #8
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
There's another point. Instead of your while loop, wouldn't this make more sense?
char choice;

do
{
    cout << "Wanna quit?" << endl;
    cin >> choice;
}
while ((uchoice | 0x20) != 'y');
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 26th, 2005, 3:16 PM   #9
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,473
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
I still like the forEVER idea.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Apr 26th, 2005, 5:31 PM   #10
brokenhope
Hobbyist Programmer
 
Join Date: Apr 2005
Posts: 126
Rep Power: 4 brokenhope is on a distinguished road
Hmm I like the ideas, but while(1) works, and the thing ooble posted, I dont really know do that well, I guess it would make sense, maybe ill improve on it when its completly done, im not that concerned with the execution of the menu at this time...

anyways, about the memory stuff, I didnt really want to limit to 1000 characters, I want you to be able to enter a near unlimited amount... is that possible?
brokenhope is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:26 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC