-6

I am trying to achieve the following in C. I have ten objects, each of them have a different number assign to them and they have their own value as well .For example 1 = 1000. While the user input 1, it should reply the value of 1 which is 1000. where do i need to defied the object? in INT or using char? Also it seems that i can't int = an number why is that?

Here is the unfinished code that I am working with.

#include <stdio.h>
void main()
{
    int "1" ;

    "1= 1000";
    scanf( "%d ",&number2 );
    number2 = c;
    if ( c == 1)
        printf(,1)
}
lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75
butbut
  • 47
  • 1
  • 8
  • 1
    lol, grasshopper....So many problems. "1" is not a number. 1 is a number. int iNumber = 1; would help... – Grantly Jan 17 '16 at 14:08
  • Concentrate on declaring your variables. int "1"; is not valid syntax and does nothing – Grantly Jan 17 '16 at 14:09
  • Which resource are you using to learn C? The reason I ask is that you're obviously having problems with it, and I'm trying to discern which one of you or the resource is confused. – autistic Jan 17 '16 at 14:09
  • What is `c`? where is it declared? please post the actual code. And `void main()` is wrong. – Iharob Al Asimi Jan 17 '16 at 14:09
  • @Seb I learn it from internet different website, – butbut Jan 17 '16 at 14:17
  • 2
    Find a different internet different website. – Martin James Jan 17 '16 at 14:17
  • @iharob that is the actual code, i write it my self. – butbut Jan 17 '16 at 14:18
  • @MartinJames any suggestions? – butbut Jan 17 '16 at 14:18
  • It sounds like you should actually have an array of ten numbers. Then you can use indexing for "naming" each number. I would recommend [a good book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) as internet tutorials are rarely any good. – molbdnilo Jan 17 '16 at 14:18
  • 1
    @butbut it doesn't compile. It's just a little code with a lot or errors, it has more errors than code. Who taught you to write that code? where did you learn from? – Iharob Al Asimi Jan 17 '16 at 14:19
  • 1
    The lines with just brackets have no errors. Seriously, OP, if you are not trolling, you need a LOT more experience before posting questions here. – Martin James Jan 17 '16 at 14:20
  • If @molbdnilo is right, maybe you need a const array for lookup, eg: 'const int lookup[] = {0, 1000, 2000, 3000.......};' ?? – Martin James Jan 17 '16 at 14:24
  • @butbut Anyone can post anything on the internet. People often write tutorials and put them on the internet to make themselves feel good, not necessarily to teach correctly. Most of the time, they haven't even fully learnt about the topic they're teaching... **Get a book**! K&R2E is good, but don't pirate it like people here are suggesting. If money is a problem, go to the library and borrow it. – autistic Jan 18 '16 at 05:39

3 Answers3

1

There are some problem in this code :

1) According to the standard you shouldn't use

void main()

But either

int main()
int main(void)
int main(int argc, char *argv[])

Also never forget

return 0;

Sample program :

#include <stdio.h>

int main(void) {       
    /* Do stuff */        
    return 0;    
}

If you wish to read more information about that, click here.

2)

int "1" ;

That's not a correct way to declare a variable. You should have write :

int variable_name = 1;

Moreover, if you want to declare an array of 10 integer elements you have to write :

int array[10] = {....}

3)

"1= 1000";

I guess you want to overwrite the value of the variable prior declared with "1". Following my example given before :

variable_name = 1000;

4)

scanf( "%d ",&number2 );

number2 = c;

You didn't declare either the variable number2 nor c.

5)

printf(,1)

That's not how you use printf. My guess here is that you tried to print :

int "1" ;

Following my example you can achieve this doing:

printf("%d", variable_name);

EDIT : My advices given above are corrects. Perhaps you was looking for something like that :

#include <stdio.h>

int main(void){

        int array[10] = {1000,2000,3000,4000,5000,6000,7000,8000,9000,10000}, user_choiche = 0, sum = 0, i = 0;

        do
        {
                printf("\nEnter a valid position (0 <= n <= 10) : ");
                scanf("%d", &user_choiche);
        }
        while(user_choiche < 0 || user_choiche > 10);

        printf("\nThe value stored in the %d position of the array is : %d\n\n", user_choiche, array[user_choiche]);

        for(i = 0; i < 10; i++)
             sum += array[i];

        printf("\nSum is %d.\n\n", sum);


return 0;

}

Input :

Enter a valid position (0 <= n <= 10) : -2

Enter a valid position (0 <= n <= 10) : 2

Output :

The value stored in the 2 position of the array is : 3000

Sum is 55000

Community
  • 1
  • 1
Claudio Cortese
  • 1,372
  • 2
  • 10
  • 21
  • Yes that is what I want. Thanks. Is it possible while i enter two number for example 1 2 and it will add up the first & second number ? – butbut Jan 17 '16 at 15:44
  • @butbut what do you want to do exactly? – Claudio Cortese Jan 17 '16 at 15:49
  • `return 0` is not required for `main`. OP apparently needs a (different) learning resource. SO is not tutorial site. – too honest for this site Jan 17 '16 at 15:57
  • @Olaf This is the first time I hear that return 0 is not required in main. Why you say so? – Claudio Cortese Jan 17 '16 at 15:59
  • @ClaudioCortese: Please read a good C book and/or tutorial. No offence, but from your code and questions, you are missing even the most basic concepts of programming in general and C in special. – too honest for this site Jan 17 '16 at 16:00
  • Quoting : "It returns the 0 to OS to tell the OS that your program executed successfully." – Claudio Cortese Jan 17 '16 at 16:01
  • I want to write an c program that can read 5 input integers representing the value of the object. The program should output the sum of the value for all of the objects, error msg if the position number entered is wrong (for example the range is 1-10, and i entered 11 and there should be an error message), and an error message while the input numbers are less than 5. – butbut Jan 17 '16 at 16:02
  • @butbut then store this 5 value you take as input from the user, use a for loop to sum all the numbers of the array – Claudio Cortese Jan 17 '16 at 16:26
  • @butbut I've edited my code with the sum of the elements – Claudio Cortese Jan 17 '16 at 16:45
  • @ClaudioCortese: If that comment did address me, you sjould add `@`. For returning from `main`. the C standard is very clear about that. You do **not** need to explicitly return `0`. http://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.3p1 – too honest for this site Jan 17 '16 at 20:09
  • @Olaf then there is something odd with gcc, if I don't use return 0; in main it gives me some warnings, that's why I assumed that return 0 should be included. I'm using -Wall -W -Wextra -pedantic – Claudio Cortese Jan 17 '16 at 20:15
0

You should honestly put more effort before posting something like that. You and I have got something in common, we are both noobs, yet that is not an excuse for not putting the right ammount of effort beforehand. Do some research before asking a question, most of the times, you will find out that it is in your power to solve it, the secret is to persevere and not take things for granted. Here, this will help for starters:

http://www.catb.org/~esr/faqs/smart-questions.html (we are not a help desk for your project!)

-1

Try this :

int arraynumbers[11] = {0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
int input;
scanf("%d",&input);
printf("%d", arraynumbers[input]);
Christian Vibora
  • 103
  • 1
  • 11
  • You are trying to initialize a 10 elements array with eleven elements warning: excess elements in array initializer [enabled by default] int arraynumbers[10] = {0,1000,2000,3000,4000,5000,6000,7000,8000,9000,10000} warning: (near initialization for ‘arraynumbers’) [enabled by default] – Claudio Cortese Jan 17 '16 at 15:40
  • So what? Just remove one initializer value! – Martin James Jan 17 '16 at 17:01
  • Amazing - two downvotes just because of one extra value in an initialiizer list? I can cancel one out:) – Martin James Jan 17 '16 at 17:11
  • @Claudio Cortese you obviously young student. It's just a code snippet and you try to compile it. – Christian Vibora Jan 17 '16 at 21:44
  • @Martin James you're right, it's NEVER a problem if you know what you're doing. :) – Christian Vibora Jan 17 '16 at 21:45
  • Of course I know what that mean, and I posted it as a comment to make you fix your code. By the way yes, I'm a young student, but that doesn't mean I don't know the basilar things. – Claudio Cortese Jan 17 '16 at 21:50
  • As time goes by, basilar things will blur in your head. It's confusing because array[10] have 11 indexes. – Christian Vibora Jan 17 '16 at 21:59