![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Apr 2005
Posts: 126
Rep Power: 4
![]() |
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? |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
the easiest way to do a loop like you want is to start it with this expression here
while(1)
{
}break; you also use the keyword continue; 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. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Apr 2005
Posts: 126
Rep Power: 4
![]() |
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... |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Apr 2005
Posts: 77
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
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. |
|
|
|
|
|
#6 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Don't use gets - instead, use scanf or fgets, which both include ways to disregard more than x characters:
scanf("%999s", ¬e);
OR
fgets(note, 999, stdin); |
|
|
|
|
|
#7 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#8 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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'); |
|
|
|
|
|
#9 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Apr 2005
Posts: 126
Rep Power: 4
![]() |
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? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|