7 Replies - 854 Views - Last Post: 22 October 2018 - 09:07 AM Rate Topic: -----

#1 Ivanofff   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 22-September 18

(C) Noob problem

Posted 21 October 2018 - 05:17 AM

Hi, I wanted to write a programm with if(), in which when I type a first letter of a country-name, I will get info about it`s population and how many cities there are.
But when I type every letter, I get info only about Algeria
Attached Image
Please help. :surrender:
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int pop, cities;
    char* cname;
    printf("Type a letter a-e: ");
    char name[0];
    scanf("%s", &name[0]);

    if(name[0] = 'a')
    {
        cname = "Algeria";
        pop = 42200000 ;
        cities = 84;
    }
    else if(name[0] = 'b')
    {
        cname = "Bolivia";
        pop =  11248864;
        cities = 29;
    }
    else if(name[0] = 'c')
    {
        cname = "Canada";
        pop = 37067011 ;
        cities = 132;
    }
    else if(name[0] = 'd')
    {
        cname = "Denmark";
        pop =  5785864;
        cities = 30;
    }
    else if(name[0] = 'e')
    {
        cname = "England";
        pop =  55619400;
        cities = 156;
    }
    else
    {
        printf("\nWrong letter");
    }
    printf("\n%s has", cname);
    printf(" %d cities.", cities);
    printf(" There live %d people", pop);
}


Is This A Good Question/Topic? 0
  • +

Replies To: (C) Noob problem

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6502
  • View blog
  • Posts: 22,257
  • Joined: 05-May 12

Re: (C) Noob problem

Posted 21 October 2018 - 05:43 AM

You are using the assignment operator = instead of the equality operator == in your if statements.
Was This Post Helpful? 2
  • +
  • -

#3 Ivanofff   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 22-September 18

Re: (C) Noob problem

Posted 21 October 2018 - 06:04 AM

View PostSkydiver, on 21 October 2018 - 05:43 AM, said:

You are using the assignment operator = instead of the equality operator == in your if statements.

Thank You, man
Was This Post Helpful? 0
  • +
  • -

#4 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 120
  • View blog
  • Posts: 849
  • Joined: 05-December 13

Re: (C) Noob problem

Posted 21 October 2018 - 06:26 AM

Ivanofff, can you please use descriptive titles to your problem. 'Noob problem' doesn't really tell anyone what your issue is. You will also get more interactive contributions with a relative title.
Was This Post Helpful? 2
  • +
  • -

#5 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2238
  • View blog
  • Posts: 4,325
  • Joined: 30-May 10

Re: (C) Noob problem

Posted 21 October 2018 - 08:32 AM

> 08 char name[0];
> 09 scanf("%s", &name[0]);
Pop quiz: How many chars can you store in an array with zero elements?
C will let you type in as many as you like, regardless of the space available.
C will not care one jot about any other variable which gets trashed as a result of enthusiastic users.

11 if(name[0] = 'a')
Like I said previously, get a better compiler.
This is another error that would have been spotted at compile time, rather than leaving you wondering "huh?" at run-time.
Was This Post Helpful? 2
  • +
  • -

#6 Ivanofff   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 22-September 18

Re: (C) Noob problem

Posted 21 October 2018 - 01:17 PM

View PostSalem_c, on 21 October 2018 - 08:32 AM, said:

Like I said previously, get a better compiler.
This is another error that would have been spotted at compile time, rather than leaving you wondering "huh?" at run-time.

Which compiler would you recommend?
I use CodeBlocks with it's GNU Compiler

View PostSheepings, on 21 October 2018 - 06:26 AM, said:

Ivanofff, can you please use descriptive titles to your problem. 'Noob problem' doesn't really tell anyone what your issue is. You will also get more interactive contributions with a relative title.

My titles are like sensations that attract more people :rolleyes:
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6502
  • View blog
  • Posts: 22,257
  • Joined: 05-May 12

Re: (C) Noob problem

Posted 21 October 2018 - 01:58 PM

You already have an excellent compiler. Just turn up the warning levels and pay attention to the warnings returned by it.
Was This Post Helpful? 1
  • +
  • -

#8 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2238
  • View blog
  • Posts: 4,325
  • Joined: 30-May 10

Re: (C) Noob problem

Posted 22 October 2018 - 09:07 AM

> I use CodeBlocks with it's GNU Compiler
Select the project build options.
Attached Image

You can add -Wextra while you're there.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1