problem in c program

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

problem in c program

Post by J-Progress on Sat Nov 14, 2015 11:56 am
([msg=90577]see problem in c program[/msg])

http://postimg.org/image/9ms0bpjij/

hello,
in the image above is a part of c program that get as input the age, name and surname in one alphanumeric vaiable (23 john something) and the address in a second (address xxx xxxxx).Then the user gives ane age and the program shows the names with that age.The problem is that the brogram i have write crashes. What's wrong. Thanks in advance!
J-Progress
New User
New User
 
Posts: 5
Joined: Thu Nov 05, 2015 4:54 pm
Blog: View Blog (0)


Re: problem in c program

Post by pretentious on Sun Nov 15, 2015 1:30 am
([msg=90583]see Re: problem in c program[/msg])

Do u get an error or something? Not familiar with the math library but you're not using any functions that would make the program panic and do anything stupid. Methinks logical error.
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
Can you say brainwashing It's a non stop disco
User avatar
pretentious
Addict
Addict
 
Posts: 1203
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)


Re: problem in c program

Post by J-Progress on Sun Nov 15, 2015 5:11 am
([msg=90584]see Re: problem in c program[/msg])

when i compile it it just says: [warning]comparison between pointer and integer. It crushes exactly when i enter the age to find the name.
J-Progress
New User
New User
 
Posts: 5
Joined: Thu Nov 05, 2015 4:54 pm
Blog: View Blog (0)


Re: problem in c program

Post by -Ninjex- on Sun Nov 15, 2015 7:41 am
([msg=90586]see Re: problem in c program[/msg])

Frankly, I'm tired of people trying to get help with their source code and posting images instead of code blocks....
I'm not going to make an OCR program to convert your images over to text files, so please post code blocks and error outputs when asking for help.
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: problem in c program

Post by J-Progress on Sun Nov 15, 2015 9:45 am
([msg=90588]see Re: problem in c program[/msg])

ok sorry your right...


Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define pl 2

int main() {
    int i,j,j1,abc;
    char an[pl][30], add[pl][30], ag[3], age[pl][3];
    for(i=0;i<pl;i++) {
                     printf("Give age, name and address\n");
                     
                      gets(an[i]);                                             //stores age name surname
   
                      gets(add[i]);                                            //stores address
                      }
    printf("Give an age\n");
    gets(ag);                                                             //stores age user want to find
   
     
    for(i=0;i<=pl;i++){
                     abc=1;
                     for(j=0;an[i][j]!=" ";j++){
                                       j1=j+1;
                                       if(ag[i]!=an[i][j]){
                                                            abc=0;
                                                }
                                          }
                  if(abc=1){
                              for(j=j1;an[i][j]!="\0";j++){
                                                           printf("%s", an[i][j]);
                                                    }
                           }
       
                     }
                                
                                                              

    system("pause");
    return 0;
    }


and i get this warning: [warning]comparison between pointer and integer
J-Progress
New User
New User
 
Posts: 5
Joined: Thu Nov 05, 2015 4:54 pm
Blog: View Blog (0)


Re: problem in c program

Post by boriz666 on Mon Nov 16, 2015 9:14 am
([msg=90615]see Re: problem in c program[/msg])

Greetings Sir,
you have several errors in your program that might not be aparant to the untrained eye.

1)
for(i=0;i<=pl;i++){
Your for loop here goes out of bounds it will iterate through first([0]) second([1], third[2]) element
eventhough the array is only defined to hold 2 elements.
It should instead be:
for(i=0;i<pl;i++){

2) if(abc=1){
Will always be true as you are making an assignment and not a test for equality as i supposed you
wish to do. It should instead be:
if(abc == 1){

3) When you are comparing chars, you cannot use " " which means a char*, but use ' ' instead
for(j=0;an[i][j]!=" ";j++){ change to : for(j=0;an[i][j]!=' ';j++){
for(j=j1;an[i][j]!="\0";j++){ change to: for(j=j1;an[i][j]!='\0';j++){

4) in your second main for loop you are iterating through the 2 records, which is fine, but
you have your inner comparison wrong:
if(ag[i]!=an[i][j]){
ag[i] is the first character in ag for the first record and the second character in ag in the second
iteration, that is propably not what you want.

5) in your result printing loop:
if(abc==1){
for(j=j1;an[i][j]!='\0';j++){ // get rid of this, see text below
printf("%s", an[i][j]);
} // get rid of this, see text below

you want to print each character out from the point after the age is found (i persume)
but you use %s instead of %c, you need to use %c cause its char's you wish to print.
here you could also use: printf("%s",an[i][j1]);
and then get rid of the for loop as i wrote in comments above

Other than that the initial prompt when running the program is unclear to the user,
try to make it clear that on the first line the user should write his age, name and surname
and on the second line he should write his address. Its only clear in the code comments not
in the text given to the user.
boriz666
Experienced User
Experienced User
 
Posts: 99
Joined: Tue Mar 24, 2015 11:53 am
Blog: View Blog (0)


Re: problem in c program

Post by J-Progress on Tue Nov 17, 2015 4:52 pm
([msg=90636]see Re: problem in c program[/msg])

Thank you very much for your help... i think it works as i want...
J-Progress
New User
New User
 
Posts: 5
Joined: Thu Nov 05, 2015 4:54 pm
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests