Thread: can someone help me out? i have no idea what to do

  1. #16
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    678
    So sorry to hear this. YouTube videos are notoriously horrible for teaching C effectively.

    Short of learning C in a class given by a qualified instructor, I would recommend studying one of two books:

    C Primer Plus (6th Edition)
    Stephen Prata author

    C Programming: A Modern Approach, 2nd Edition K. N. King author

    I think you need to study one of these books, and doing the exercises at the end of each chapter, before attempting the program in the OP.

    Good luck, and feel free to ask us questions as you study the book!

  2. #17
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Code:
    int main(array<System::String ^> ^args)
    {
    
    char sentence[100];
    char toReplace[20];
    char replacement[20];
    
    printf("Enter a sentence: ");
    fgets(sentence, 100, stdin);
    
    char *pos = strstr(sentence, toReplace);
    
    
    while (pos == NULL){
    	printf("Enter the word you want to replace: ");
    	scanf("%s",&toReplace);
    	pos = strstr(sentence, toReplace);
    	if(pos != NULL)
    		break;
    
    	printf("! Error: %s does not exist.\n", toReplace);
    }
    // if found
    if (pos != NULL) {
    
    	printf("Enter the new word: ");
    	scanf("%s",&replacement);

    Okay, I solved 90% of this problem. Here is the first half of my main, see if you can complete the rest. I will try to finish it as well (just to see how do to it myself, since I am also (re)learning c).
    Last edited by Terrance; 03-19-2017 at 11:31 AM.

  3. #18
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    678
    Quote Originally Posted by Terrance View Post
    Okay, I solved 90% of this problem. Here is the first half of my main, see if you can complete the rest. I will try to finish it as well (just to see how do to it myself, since I am also (re)learning c).
    We are here to guide the poster on how to solve the problem they present, not to do the programming for them.

    Having tried to learn C from painfully inadequate YouTube videos, the OP is not yet ready to attack the problem, even with the code you present. And by the way, you have not solved 90% of the problem.

  4. #19
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Quote Originally Posted by rstanley View Post
    We are here to guide the poster on how to solve the problem they present, not to do the programming for them.

    Having tried to learn C from painfully inadequate YouTube videos, the OP is not yet ready to attack the problem, even with the code you present. And by the way, you have not solved 90% of the problem.
    Hi, I only gave the code for the portion which was solved using advice on this message board: using string functions, using fgets, and using strstr. But what I showed was only half of my main, but I have 90% of the problem solved (but I did not post the rest of the code). I'm just missing a user defined function, and one of the strlen's in my code isn't counting correctly, so my replace word is getting cut off. But once I solve those two parts of this problem, I have the answer.

  5. #20
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    Hi, I only gave the code for the portion which was solved using advice on this message board: using string functions, using fgets, and using strstr.
    You do realize that your "fixed" code has at least one place that invokes undefined behavior, right? You do know that you also have several places where you have possible buffer overrun errors waiting to happen, right (never use a function to retrieve a C-string that doesn't limit the number of characters retrieved)?

    By the way what happens if your fgets() fails to retrieve a complete line? After all 100 characters for a line is fairly low these days.

    Jim

  6. #21
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Quote Originally Posted by jimblumberg View Post
    You do realize that your "fixed" code has at least one place that invokes undefined behavior, right? You do know that you also have several places where you have possible buffer overrun errors waiting to happen, right (never use a function to retrieve a C-string that doesn't limit the number of characters retrieved)?

    By the way what happens if your fgets() fails to retrieve a complete line? After all 100 characters for a line is fairly low these days.

    Jim
    I learned c in the academic setting close to 15 years ago, so I do understand what you mean. But I was merely trying to solve the problem, as I am relearning c myself. As I continue to get more studying time with c, and gain more experience, I probably won't make those types of mistakes. But for now, I'm a beginner again, unfortunately. But thank you for pointing out those "errors waiting to happen."

  7. #22
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    678
    Quote Originally Posted by Terrance View Post
    Hi, I only gave the code for the portion which was solved using advice on this message board: using string functions, using fgets, and using strstr. But what I showed was only half of my main, but I have 90% of the problem solved (but I did not post the rest of the code). I'm just missing a user defined function, and one of the strlen's in my code isn't counting correctly, so my replace word is getting cut off. But once I solve those two parts of this problem, I have the answer.
    We provide words of advise to the OPs, so the OPs can solve the problem themselves! We point out errors in the code, and advise such as why the use of gets() and System("pause") are not recommended. We do NOT do the coding for them. The OP cannot learn to code C if another person posts their solution to the code. You are not the OP.

    If you wish to code the solution yourself, offline as a learning experience, great!

    The same is true of any subject.

  8. #23
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Quote Originally Posted by rstanley View Post
    We provide words of advise to the OPs, so the OPs can solve the problem themselves! We point out errors in the code, and advise such as why the use of gets() and System("pause") are not recommended. We do NOT do the coding for them. The OP cannot learn to code C if another person posts their solution to the code. You are not the OP.

    If you wish to code the solution yourself, offline as a learning experience, great!

    The same is true of any subject.
    Sorry about that. I thought the problem was interesting, since I'm learning c and incorporating the c standard library myself, so I put some time into solving this problem as well. But I will try to make sure that I give advice to get the OP to solve the problem, rather than do the coding for them.

  9. #24
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    But thank you for pointing out those "errors waiting to happen."
    Do you understand the Undefined Behavior problems (there are several)? This is really a much more serious problem than the possible buffer overflow problems. You need to thoroughly study the documentation for strstr(), fgets(), and scanf().

    You should also understand what will happen to your code if the user enters a sentence is larger than the "limit" you placed in your fgets() call, especially if the "sentence" is larger than both the fgets() limit, and the size of the toReplace array. Remember the limit placed on the fgets() call only limits what the system will try to place in the array, not how many characters are actually entered by the user and those extra characters will try to go someplace.

    Jim

  10. #25
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Terrance is an idiot. It's not even C code. What a fool.

  11. #26
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Quote Originally Posted by jimblumberg View Post
    Do you understand the Undefined Behavior problems (there are several)? This is really a much more serious problem than the possible buffer overflow problems. You need to thoroughly study the documentation for strstr(), fgets(), and scanf().

    You should also understand what will happen to your code if the user enters a sentence is larger than the "limit" you placed in your fgets() call, especially if the "sentence" is larger than both the fgets() limit, and the size of the toReplace array. Remember the limit placed on the fgets() call only limits what the system will try to place in the array, not how many characters are actually entered by the user and those extra characters will try to go someplace.

    Jim
    Yes, I understand, although I took my c course back in 2002. For the undefined behavior, yes, I don't understand the functions I'm using well enough to use them without getting warnings, but I will make sure that I will study the c functions in more depth. I allocated enough space in the char arrays that I wrote, to get the problem solved. But I do under stand computer architecture, and I do understand that an array with a specifically defined size can encounter problems if a user tries to put more data in the character array list than what it can actually hold. But again, I'm relearning c, so I am not too overly focused on specifics. But I do understand arrays, lists, allocating memory space, and how to get more information on built in library functions. But I have not coded that much in the past 10 years, so my focus is now on coding. And in terms of my program not being c code, I wrote the code in visual C++ express, but the syntax is all c (I believe).
    Last edited by Terrance; 03-19-2017 at 01:06 PM.

  12. #27
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    I wrote the code in visual C++ express, but the syntax is all c (I believe).
    Nope, fails this on the very first line.

    By the way you really seem to have no idea about the Undefined Behavior that I'm hinting at. Like I said you really really need to study the documentation for the standard functions you're trying to use, your calls to scanf() is invoking Undefined Behavior I doubt you can really tell me why. You need learn to properly initialize variables before you try to use them as well.

    But again, I'm relearning c, so I am not too overly focused on specifics.
    This is a very very very bad bad bad idea. Programming is all about specifics. If you don't understand the specifics of the problem your program will probably never specifically solve the intended problems.

    Jim

  13. #28
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    I will studying the documentation better for the functions. Yes, I don't know the specifics of the undefined behavior.

  14. #29
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    Well using a decent compiler that is properly configured would probably be a good start. Trying to use the Microsoft C compiler is like using a fossil, I suggest you investigate getting another C compiler, one that truly supports the current C standards, instead of one that only truly supports C90. Also make sure you're actually using C, not some other hybrid language.

    If you're compiler was properly configured, it should be warning you about most of the problems with your code. Also remember that using a variable before you have assigned a value to it invokes Undefined Behavior.

    Lastly when you finally study the documentation be sure you pay particular attention to what happens when a scanf() format specifier doesn't match the type.

    Jim

  15. #30
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    I've been having issues with other compilers, so visual c++ express was the one that has worked the best for me. I've also been busy with work, and I would also like to learn Python, so c is also almost a side subject for me right now. But it was my first non scripting language that I learned. So I do still remember most of what I learned about c (in school), but it was so long ago, I just feel like I'm hacking away at the code sometimes. But I will make sure to study the function libraries to make sure I understand why I'm getting the warnings.
    Last edited by Terrance; 03-19-2017 at 02:27 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My idea.
    By Zach Sisk in forum C Programming
    Replies: 39
    Last Post: 09-17-2012, 02:43 PM
  2. No idea on how to do this...
    By Sembhi in forum C++ Programming
    Replies: 11
    Last Post: 12-05-2007, 08:26 PM
  3. Idea
    By RPW in forum Contests Board
    Replies: 0
    Last Post: 01-07-2003, 07:50 AM
  4. Good idea, bad idea.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-15-2002, 12:26 PM

Tags for this Thread