OKay I get what you guys are saying and I did that first
but then nothing runs at all atleast first I was getting an enter your string valueCode:if(strcmp(command ,"new")==0) { getString(string);
I know I have some small error somewhere or some concept I don't get but could you please tell me why my output isn't coming right
Code:#include <string.h> #define SIZE 200 #define HISTORY_SIZE 10 /*the maximum size of the history*/ //#define MAX_STRING_LEN 100 /*the maximum size of strings*/ #define MAX_CMD_LEN 10/*the maximum size of a command*/ void getString( char* ); void printString (const char *); void appendString(char *str, char *newstr); int main() { // char buf [BUFSIZ]; char command[MAX_CMD_LEN]; char string[SIZE]; char newstr[SIZE]; char *p; int flag; do { printf("cmd> "); if(fgets(command, MAX_CMD_LEN, stdin) != NULL) if((p = strchr( command, '\n')) == NULL) *p = '\0'; if(strcmp(command ,"new")==0) { getString(string); } // if((string = strchr(buf, '/n')) != NULL){ // *string = \0; if(strcmp(command, "list")==0) { printString(string); } if(strcmp(command, "append")==0) { appendString(string, newstr); } } while (flag==1); return 0; } void getString(char *string) { printf("Please enter your string"); fgets(string, SIZE, stdin); } void printString (const char *string) { // if(fgets(buf, SIZE(buf), stdin != NULL){ printf( "\n\nHere is the text you entered:\n%s\n", string); // } //} } void appendString(char *string, char *newstr) { printf("Enter string to append"); fgets(newstr, SIZE, stdin); strcat(string,newstr); fgets(string,SIZE, stdin); }
Last edited by kiwi101; 10-23-2012 at 08:21 PM.
Is it because of my while part of the do while loop, if so what should I write?
Yes, your do while(flag==1) is a problem, because you haven't assigned a value to flag yet. make it int flag=1; right near the top of the program.
Everything in your program will be flowing out and returning to that big do while() loop, and that's the way it should be. Call other functions to keep the size of it, reasonable.
So when i intialized the flag =1 the cmd> printed perfectly everytime I wrote enter just as it should. But thats all that happens. If it write new "Enter your string" doesn't pop up. Unless I change the
but i still get the wrong answerCode:if(strcmp(command ,"new")!=0)
i know im really close to getting this code and its frustrating trying to figure out my mistakes
PLEASE HELP!!!
Last edited by kiwi101; 10-23-2012 at 08:30 PM.
It's great fun to try different things with C, but when you are serious about working on a program, it's time to limit the guessing on this, guessing on that, approach.
Take it step by step (don't jump ahead of what you're doing atm), and don't go crazy with guesses and emotions. Let's see what's up now.
Let's change this part, from == NULL, to != NULL, and add the curly braces to it as well. The curly braces aren't required, but your other if statements have them, and it's VERY good to be consistent and clear in your code and coding style.
Give that a try.Code:if(fgets(command, MAX_CMD_LEN, stdin) != NULL) { //add curly braces if((p = strchr( command, '\n')) != NULL) //change from == NULL to != NULL *p = '\0'; } //add curly brace.
Last edited by Adak; 10-23-2012 at 08:47 PM.
You tell me I'm out of ideas of what could be wrong
Post up your new code, after you make the changes I mentioned. Report your next problem. Step by step, meter by meter, life is sweeter.
OMGSH your a lifesaver!
i don't know why i didn't think of that before
do you know why my list doesn't print out my "appended function"
it prints out the new just fine
Nevermind got it had to change my function append
THANK YOU!!!