Why is this code not working

This is a discussion on Why is this code not working within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> int main() { double day[7]; char** days = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"}; ...

  1. #1
    1337
    Join Date
    Jul 2008
    Posts
    135

    Why is this code not working

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
        double day[7]; 
        char** days = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
        int i;
        double totalExp;
        for (i = 0; i < 7; i++){
            printf ("Please enter you expenditure for %s: ", days[i][]);
            days[i++][];
            scanf("%f", day[i]);
            fflush(stdin);
            totalExp += day[i];
        }
        printf ("total expenditure is: %f", totalExp)
        getchar();
        return 0;
    }
    I think the problem relies on the array of pointers.
    Last edited by valthyx; 08-16-2009 at 11:48 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you had an array of pointers, you might be somewhere -- in fact I would suggest you use an array of pointers. "char **" is, alas, not such a beast.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    That code can't compile. [] with nothing between is not valid when used in that context.

    Also read the FAQ about how you should NOT use fflush(stdin);

    To learn about arrays vs pointers it might help for you to first understand the difference between:
    Code:
    char *x = "hello world";
    and
    Code:
    char y[] = "hello world";
    x is a pointer here, but y is an array.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    1337
    Join Date
    Jul 2008
    Posts
    135
    What i am trying to do here is print "sunday: " then the user inputs the value, then print "monday: ", then the user inputs the value, and so on.

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by valthyx View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
        double day[7]; 
        char** days = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
        int i;
        double totalExp;
        for (i = 0; i < 7; i++){
            printf ("Please enter you expenditure for %s: ", days[i][]);
            days[i++][];
            scanf("%f", day[i]);
            fflush(stdin);
            totalExp += day[i];
        }
        printf ("total expenditure is: %f", totalExp)
        getchar();
        return 0;
    }
    I think the problem relies on the array of pointers.
    What's this?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    1337
    Join Date
    Jul 2008
    Posts
    135
    What i was trying to do was to increment the row, like. after sunday, monday, then tuesday and so forth. I am not used to 2 dimensional array. Please help. Thanks.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Maybe this example will help:
    Code:
    #include <stdio.h>
    
    int main(void) {
    	int i;
    	char *days[] = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
    	for (i=0; i<7; i++) {
    		printf("%s\n",days[i]);
    	}
    	return 0;
    }
    NEVER USE fflush(stdin).

    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Last edited by MK27; 08-17-2009 at 07:36 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    1337
    Join Date
    Jul 2008
    Posts
    135
    Owh, thanks. By the way, how if i want to make an increment along the column. For example, in the first row, i would like to print s, then u, then n till y.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by valthyx View Post
    Owh, thanks. By the way, how if i want to make an increment along the column. For example, in the first row, i would like to print s, then u, then n till y.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
    	int i, j, len;
    	char *days[] = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
    	for (i=0; i<7; i++) {
    		len = strlen(days[i]);
    		for (j=0;j<len;j++) printf("%c ",days[i][j]);
    		printf("\n");
    	}
    	return 0;
    }
    *days[] is an array of pointers, so days[i] is a char pointer & you can access it's elements using [subscript notation].
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    1337
    Join Date
    Jul 2008
    Posts
    135
    Thanks a lot

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Quote Originally Posted by MK27 View Post
    ...
    This
    Code:
    		len = strlen(days[i]);
    		for (j=0;j<len;j++) printf("%c ",days[i][j]);
    can be replaced with this
    Code:
    		printf("%s", days[i]);
    unless of course you specifically want spaces between each character.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by iMalc View Post
    This ... can be replaced with this .... unless of course you specifically want spaces between each character.
    That was pretty much explicit:

    Quote Originally Posted by valthyx View Post
    By the way, how if i want to make an increment along the column. For example, in the first row, i would like to print s, then u, then n till y.
    @iMalc: btw you are on my top 3 list for this particular offence...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MK27 View Post
    That was pretty much explicit:
    I'm with Malc; I don't see why you think that means he wants spaces in between, especially since there were three or four other places where he explicitly stated he didn't.

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    U

    Quote Originally Posted by tabstop View Post
    I'm with Malc; I don't see why you think that means he wants spaces in between, especially since there were three or four other places where he explicitly stated he didn't.
    You're wrong. And primarily what I meant was that "the advice" iMalc was offering was an almost exact duplication of post #7

    Quote Originally Posted by MK27 #7
    printf("%s\n",days[i]);
    Quote Originally Posted by iMalc #11
    printf("%s", days[i]);
    presented, I suppose, as a response to #9 -- presuming that #2-8 did not exist, since #9 was a development of #7 based on the OP's question in #8 regarding how to iterate thru each row character by character, I believe "%c " demonstrates this unequivocally.

    iMalc did not contribute a single atom of new information to the thread because he did not read it to start with, and neither, evidently, did tabstop. I actually quoted the "character by character" part in #12 but I have to guess you didn't read that, since it makes your response seem totally ridiculous ("2+2=4?" "No, 2+2=4."). From now on you might want to just address "the straw dog" to be safe

    Not that that matters much, but I can admit when I have made a mistake due to inattention: you might as well also.
    Last edited by MK27; 08-17-2009 at 05:24 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MK27 View Post
    You're wrong. And primarily what I meant was that "the advice" iMalc was offering was an almost exact duplication of post #7





    presented, I suppose, as a response to #9 -- presuming that #2-8 did not exist, since #9 was a development of #7 based on the OP's question in #8 regarding how to iterate thru each row character by character, I believe "%c " demonstrates this unequivocally.

    iMalc did not contribute a single atom of new information to the thread because he did not read it to start with, and neither, evidently, did tabstop. I actually quoted the "character by character" part in #12 but I have to guess you didn't read that, since it makes your response seem totally ridiculous ("2+2=4?" "No, 2+2=4."). From now on you might want to just address "the straw dog" to be safe

    Not that that matters much, but I can admit when I have made a mistake due to inattention: you might as well also.
    I am claiming there is a difference between "printing character by character" and "printing character by character with a space in between", is what I'm claiming -- now your guess is as good as mine about which one the OP wanted. Given that he said "printing character by character", and there's also posts 4 and 6 where spaces are explicitly decried (inasmuch as "sunday: " must be printed), I'm liking my guess, but that's okay. (I will concede, because I've been reading too much detective fiction and the word keeps coming up, that your solution fit the form and Malc's didn't, so from that perspective is the superior answer.)

    You've seen me (I guess) make just as many backtracks about misreading. There were a half-dozen threads today where you could've gotten me; you just managed to pick one I had read.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code not working?
    By Elysia in forum C++ Programming
    Replies: 12
    Last Post: 04-06-2009, 01:57 AM
  2. Replies: 3
    Last Post: 02-24-2009, 07:49 PM
  3. C code not working
    By D3ciph3r in forum C Programming
    Replies: 2
    Last Post: 05-27-2005, 04:13 PM
  4. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  5. Linked List Working Code
    By Linette in forum C++ Programming
    Replies: 9
    Last Post: 01-24-2002, 11:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21