WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Small C Program  (Read 5309 times)

nomer

  • Guest
Small C Program
« on: June 21, 2012, 12:26:28 PM »
I decided that this summer would be a good time to study C. In the book that I'm reading, there was this small program:
Code: (c) [Select]
#include <stdio.h>
#include <string.h>

char tracks[][80] = {                                           //a list of songs that the user can search through
"I left my heart in Harvard Med School",
"Newark, Newark - a wonderful town",
"Dancing with a dork",
"From here to maternity",
"The girl form Iwo Jima",
};

void find_track(char search_for[]){
int i;
for (i = 0; i < 5; i++){                                                //loop through our array
if (strstr(tracks[i], search_for))                          //check if the user's search term is in this string
printf("Track %i: '%s'\n", i, tracks[i]);        //if so, the print the track number and title
}
}

int main()
{
char search_for[80];
printf("Search for: ");             
fgets(search_for, 80, stdin); //get user input
find_track(search_for);         //send user input to the function
return 0;
}
This code compiles fine under TinyCore Linux 4.5.3 and under cygwin + windows xp. Unfortunately, It doesn't run correctly under either of them.  This is very annoying, even if it is a first edition book. 
My guess is that it has something to do with "pointer decay".

Does anyone have any idea what is causing this odd bug?
(I realize that this isn't really related to Core and no one has any obligation to offer any help of any sort ;))

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11049
Re: Small C Program
« Reply #1 on: June 21, 2012, 12:32:22 PM »
Your app is also reading the new-line character.

So you type "Iwo", the program searches for
"Iwo
"

which obviously doesn't match.
The only barriers that can stop you are the ones you create yourself.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11703
Re: Small C Program
« Reply #2 on: June 21, 2012, 01:30:32 PM »
Hi nomer
Read this:
http://beej.us/guide/bgc/output/html/multipage/gets.html
It includes a nice little example on how to strip that newline character that is causing you trouble.

nomer

  • Guest
Re: Small C Program
« Reply #3 on: June 21, 2012, 09:57:35 PM »
Thank you both so much for the helpful replies. I solved the issue by changing
Quote
fgets(search_for, 80, stdin);
to
Quote
scanf("%79s",search_for);
I'll bookmark the other solution.

I'll also try to send some feedback to the author/publisher.

Cheers,
Nomer

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11703
Re: Small C Program
« Reply #4 on: June 22, 2012, 01:54:28 AM »
Hi nomer
Using scanf like that will remove the newline character. It will also only return the first word if you enter a phrase.
Using fgets and replacing the newline character with a zero as shown in the link I provided will allow your
program to match on phrases as well as individual words.

nomer

  • Guest
Re: Small C Program
« Reply #5 on: June 22, 2012, 09:23:15 PM »
Oh, I didn't consider that possibility. Thanks for pointing that out.

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11049
Re: Small C Program
« Reply #6 on: June 23, 2012, 05:32:22 AM »
Welcome to the low-level land ;)
The only barriers that can stop you are the ones you create yourself.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11703
Re: Small C Program
« Reply #7 on: June 24, 2012, 11:41:16 AM »
Hi nomer
If you install   gdb.tcz   you will be able step through a program, examine/change variables, and set breakpoints.
Open a terminal and stretch it to twice its normal size, then:
1. Compile your program using the   -O0 -g   options, do not strip the program.
2. gdb -tui YourProgram
3. br main                (Sets a breakpoint at main)
4. r                             (Short for run)
The top half of the window displays your source code. The next line to be executed is highlighted. Here is a good
tutorial on the basics:
http://beej.us/guide/bggdb/
I've found gdb to be handy when a program does exactly what I told it to do, instead of what I wanted it to do.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11703
Re: Small C Program
« Reply #8 on: June 24, 2012, 01:20:35 PM »
Hi nomer
Since you are doing screen I/O, read the section called   Separating the Input/Output    in this link:
http://dirac.org/linux/gdb/07-Debugging_Ncurses_Programs.php
This will keep the debugger screen from getting messed up by the printf statement.