Thread: Output problem

Page 1 of 3 123 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Exclamation Output problem


    greetings everyone!!!
    here is the program
    #include <stdio.h>
    #include <ctype.h>
    void change(char *p);

    int main()
    {
    char name[30],ch;


    for(int i=0;i<30;i++)
    {
    name[i]=getchar();
    if(name[i]=='\n')
    break;
    }

    printf("%s",name);
    printf("\n ENTER 1 TO CONFIRM AND 2 TO RE-ENTER\n\n");
    ch=getchar();
    if(ch=='1')
    {
    printf("confirmed");}
    else
    {
    change(name);}
    printf("%s",name);

    return 0;

    }
    void change(char *p)
    {
    printf("working\n\n");
    for(int k=0;k<30;k++)
    {
    *(p+k)=getchar();
    if(*(p+k)=='\n')
    break;
    }
    }

    when the function is called it should ask for input from keyboard (because i used getchar()) but it is not asking anything.why??????
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Well, I cannot tell from reading your code, since it is unreadable. Use code tags!

    Bear this in mind: input does not get read until you hit ENTER. And when you read a character, remember that unprintable characters are also characters, such as CR and LF which result from hitting ENTER.

    Read the sticky threads at the top. This is certain in the FAQ. Plus you will learn about using code tags.
  4. #3
  5. Hats off to Mr. Joseph donahue
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2009
    Posts
    752
    Rep Power
    1107
    Ok, first of all read this. and always post your code in code tags to preserve indentations.
    Originally Posted by Nikhil_me
    when the function is called it should ask for input from keyboard (because i used getchar()) but it is not asking anything.why??????
    That's because the computer does what you tell it to, not what you want it to.
    You have to tell the compiler to ask for the name if you want it to ask for the name.

    Insert a printf statement saying something like "enter a name" before the first for loop.

    Next comes:
    Code:
    for(int i=0;i<30;i++)
    {
     name[i]=getchar();
     if(name[i]=='\n')
        break;
    }
    This isn't the right way to take a string input, why not use scanf or fgets to input the name. This approach has its problems, many problems.

    I am short of time so I will emphasis on one point only, and that is why are you trying to use printf after this loop???. Do you know what Nul terminated strings are? Printf works only on Nul terminated strings. And the string you take as input takes the input one character at a time, and you dont append it with any Nul characters. Thus the printf will not know when to stop printing.. Do you get the point?
    Your code has many more problems, I will get back later if I have the time.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Exclamation


    well i know program do what it is made to do.
    getchar() is suppose to wait for input from user but in this program its not asking for input.
    if one omit the loc
    if(*(p+k)=='\n')
    break;
    then program works fine. IT DO ASK FOR INPUT but when above loc are added it does not ask for input why?????
    plz do tell me why you considered my code for array input wrong. by using that code one individually enter character in the array and there is no need of putting null character at the end. but if you think its wrong then please do tell me that I will really appreciate it........
  8. #5
  9. Hats off to Mr. Joseph donahue
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2009
    Posts
    752
    Rep Power
    1107
    Originally Posted by Nikhil_me
    well i know program do what it is made to do.
    Pay attention. There are other problems. You have problems understanding arrays and strings. I don't think you should venture much into the pointers at this instance. Your English is hardly readable. Most of the members will simply ignore your post.

    You say that your code is running if you change the function 'change' a bit.
    I say that the function won't even get called. Your program should crash before reaching the function call.

    Code:
    for(int i=0;i<30;i++)
    {
      name[i]=getchar();
      if(name[i]=='\n')
        break;
    }
    printf("%s",name); //It should crash here...Seg fault or something
    P.S. You are unable to make your question clear, judging by your user name, your native tongue must be Hindi.
    I suggest you take help of someone to form your questions better.
    Last edited by RAJ_55555; February 21st, 2011 at 07:04 AM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Lightbulb


    for(int i=0;i<30;i++)
    {
    name[i]=getchar();
    if(name[i]=='\n')
    break;
    }
    printf("%s",name);

    THIS MUCH PART IS CORRECT. NO PROBLEM , IT WORKS FINE.
    I DON'T KNOW IF YOU ARE EXECUTING THIS PROGRAM OR NOT.
    if I omit the lines
    if(*(p+k)=='\n')
    break;
    from function 'change', the program works as expected. (IT WILL NOT CRASH. I dunno if there is an inbuilt compiler in your head but you should execute it on computer first, then you will see how it actually works.)
    but on addition of previously mentioned loc , it doesn't ask for any input ( on omission it does).
    also
    a program DOES what it is made to do.(hope this is correct)
    i will appreciate if you concentrate on program stuff rather than english part ( this is c forum not point out my english mistake forum).
    thanx for your time
  12. #7
  13. Hats off to Mr. Joseph donahue
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2009
    Posts
    752
    Rep Power
    1107
    Look Nikhil, you are taking this personally. I am giving you facts, that's all. I don't care how your English is, and my English isn't all that good either. It's just passable. But if you can't explain your question, how do you expect us to give answers?
    Originally Posted by Nikhil_me
    THIS MUCH PART IS CORRECT. NO PROBLEM , IT WORKS FINE.
    I DON'T KNOW IF YOU ARE EXECUTING THIS PROGRAM OR NOT.
    And again I am telling you, I am really being very patient with you but shouting doesn't make your program correct. I am not being paid to help you, so the least you can do is have a little gratitude.

    I don't need to execute that program, I can tell from my experience that it's wrong.
    The fact that it executes without a segfault is just your luck. Let me show you:

    This is the part you claim (or shout) to be correct:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char name[30],ch;
    
    	for(int i=0;i<30;i++)
    	{
    		name[i]=getchar();
    		if(name[i]=='\n')
    			break;
    	}
    	printf("%s",name);
    	return 0;
    }
    Now run it....
    Enter some random digit inputs. If you are lucky, you should notice some funny characters as output after the actual text given as input. This happens as the printf doesn't find the NUL character, and hence it doesn't know where to stop printing in the array. If you are unlucky, it will execute properly, for now. Waiting to explode some other day.

    My hunch is that it's too much to process for you at this instance. Take your time and read what Nul terminated strings are.

    And trust me that's just the starting of the problems you have with your code.
    For example, you appear to be using C. But in this line:
    Code:
    for(int i=0;i<30;i++)
    you declare an integer inside the loop, which isn't allowed in C. (Not in C89, is allowed in C99. Do you know the difference?)
    I would be glad to help you, but only if you listen. I won't waste my time anymore if you keep on behaving like you did in your last post. If you have a doubt, ask it. If you think I am wrong, tell me why you think so. Be logical, and use Code tags from the next time.
    Originally Posted by Nikhil_me
    IT WILL NOT CRASH. I dunno if there is an inbuilt compiler in your head but you should execute it on computer first, then you will see how it actually works.
    Don't dare to disrespect me again. If you don't know something then we are here to help, but I am not an employee bear that in mind. Don't dare.

    BTW what compiler are you using?

    Comments on this post

    • the eVil piXel agrees : nikil me = "punk", RAJ 5555="punk buster"
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0
    1) you exaggerated , you should know how to give polite reply. I like maths so i help ppl in mathematics in online forums. ppl from other country who do not have english as native language ask question in a very horrible manner but I don't exaggerate like you. it sometimes becomes difficult for me to even make a guess about what they actually want to say. but I don't tell them your english is horrible or get someone to write question for you. I concentrate on maths.
    2) Believe me even i am being very patient. IF I AM WRITING IN CAPITAL LETTER, IT DOESN'T MEAN I AM SHOUTING. IT MEANS DO CONSIDER IT CAREFULLY.
    3)I don't need to disrespect anyone and no one is my employee. I am not paying you and also not asking for solution from you specifically.If you want to reply be polite.
    I follow "give respect,take respect".
    i am using Xcode (mac os x)
    i know difference between c89 and c99. mine based on c99.
    here is a program
    #include <stdio.h>
    int main()
    {
    char name[100];
    for(int i=0;i<100;i++)
    {
    name[i]=getchar();
    if(name[i]=='\n')
    break;
    }
    printf("%s",name);
    }


    OUTPUT

    [Session started at 2011-02-21 22:37:13 +0530.]
    GNU gdb 6.3.50-20050815 (Apple version gdb-1510) (Wed Sep 22 02:45:02 UTC 2010)
    Copyright 2004 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB. Type "show warranty" for details.
    This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys001
    Loading program into debugger…
    Program loaded.
    run
    [Switching to process 582]
    Running…
    THIS PROGRAMME RUNS CORRECTLY // this is input//
    THIS PROGRAMME RUNS CORRECTLY //this is output//

    Debugger stopped.
    Program exited with status value:0.
    NOTE: PROGRAM EXITED WITH STATUS VALUE 0 AND RESULT IS WHAT I WANTED SO THIS CODE WORKS FINE.
    THATS WHY I WROTE IN CAPITAL LETTERS SO THAT YOU MAY TAKE IT SERIOUSLY. I HAVE COMPILED THIS CODE ON MANY OTHER COMPILERS E.G MICROSOFT VISUAL STUDIO,PELLES C. PROGRAM NEVER CRASHES AND DO GIVE DESIRED OUTPUT.
    but you did not wanted to believe that. you don't want to try you are only relying upon your experience (though it works most of the time but not everytime).try this code on your compiler and you will definitely don't get any error or unexpected behaviour.

    Comments on this post

    • LaughingBelly disagrees : I'd love to see you use that argument when it crashes while your instructor is running it.
    • ptr2void disagrees : Ignorant, doesn't listen, and can't be arsed to use code tags.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    54
    Rep Power
    0
    Originally Posted by Nikhil_me
    NOTE: PROGRAM EXITED WITH STATUS VALUE 0 AND RESULT IS WHAT I WANTED SO THIS CODE WORKS FINE.THATS WHY I WROTE IN CAPITAL LETTERS SO THAT YOU MAY TAKE IT SERIOUSLY. I HAVE COMPILED THIS CODE ON MANY OTHER COMPILERS E.G MICROSOFT VISUAL STUDIO,PELLES C. PROGRAM NEVER CRASHES AND DO GIVE DESIRED OUTPUT.but you did not wanted to believe that. you don't want to try you are only relying upon your experience (though it works most of the time but not everytime).try this code on your compiler and you will definitely don't get any error or unexpected behaviour.
    OK I can expect that reply because the same thing happened with me(a certain snippet was working on my compiler but not on other compilers), but you should consider RAJ 5555's reply, I don't think he is going to reply again, but get this - I compiled the program in mingw and got an undesirable result. I highly suggest you listen to the people here because they won't misguide you. They are telling you the truth.

    But one more thing is that since your Xcode supports c99, this may give you results different from other compilers(c89/c90). I'll try to get pelles C to compile the snippet and give you any feedback if necessary.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    54
    Rep Power
    0
    There are serious flaws in the program, it is incorrect logically also, the only reason your compiler is not showing anything because you are lucky(as told by someone else).
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Red face hiiiiiiiiiiiiii the eVil piXel


    greetings!!!
    I know the code that I have written. I also know which kind of problem raj_5555 was talking about (i.e appearance of special characters when string not terminated by null. i have faced this problem when i first started doing c but my current code don't have that problem). I was annoyed because I have tried this coding manyyyyyy times (pelles c,borland, turbo c,xcode,microsoft visual studio) that code never gives wrong result. when I asked my main question I was not even bothering about that code but raj_5555 was over sure that it is wrong. I told him he should try it but he says he don't need to try because he has got experience. thats why finally I posted the program and the output as well.
    I do understand your concern regarding different types of compilers but maybe those I have mentioned are very common.
    I also belive that EVERY REPLY MATTERS, thats why I consider every reply seriously.
    if i am asking question it doesn't mean i am not trying practically. raj's point that "I don't need to execute I can say on the basis of my experience really irked me.
    you may try this snippet on pelles c . you will not get any error because when i was beginner i used same code snippet on pelles c and always got desired result.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Red face hiiiiiiiiiiiiii the eVil piXel


    I DO BELIEVE RESULT IS COMPILER SPECIFIC.
    I USED PRINTF IN SOME COMPILERS WITHOUT INCLUDING ANY HEADER FILE AND I GOT THE RESULT AND IN SOME I GOT ERROR.
    since printf used frequently in some compiler one don't need to include any header file. SO IN SOME COMPILER SPECIAL CHARACTER CAN APPEAR MAY BE IN XCODE IT IS SPECIFIED THAT AFTER PRESSING RETURN LAST CHARACTER WILL BE NULL SO I DON'T NEED TO SPECIFY IT EXPLICITLY.
    but my main question is being ignored i just wanted to ask why my getchar was not asking for input.
  24. #13
  25. Hats off to Mr. Joseph donahue
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2009
    Posts
    752
    Rep Power
    1107
    I was not only oversure, I still am. I wont be wasting my time on you anymore. You are not ready to learn, if you cant accept you are wrong. And I am certainly not going to correct your code partially, while the more basic problems remain.

    Hope someone else comes over and helps you out this time. Even so, your program will just be a time bomb waiting to explode. Best of luck.

    Comments on this post

    • ptr2void agrees : Don't waste your breath on this help vampire.
  26. #14
  27. Hats off to Mr. Joseph donahue
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2009
    Posts
    752
    Rep Power
    1107
    and stop using the frigging capital letters. Learn some netiquette.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Lightbulb


    ya i admit for netiquette one should use small letters (you are absolutely right here).
    you are not over sure but even i am not writing for wasting time. I have posted the output which is flawless but yes i believe that output may be different in other compilers.
    i am ready to learn thats why i have posted question in forum.
    i am not mr right i know that. i want to clear my doubt . the problem i faced in my program , in my compiler , i posted about it in the forum.
    (it was your exaggeration that irked me....but never mind)
    BTW thanks for trying I really do appreciate your efforts.( you are the first guy who took interest in the code :) ). we are having this argument due to different compiler ( mine is not giving error, you don't want to believe code is errorless).
    FOR ME :- this snippet may give error in other compilers;
    FOR YOU :- this snippet don't give error in xcode (mac os x)
    hope you will have a gr8 time
    take care
    byeeeee......... :)
Page 1 of 3 123 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo