Jump to content

For some reason this does like absolutely nothing. The print statement comes up and I type a random letter, but when I enter I just get an endless amount of blank lines.

 

post-33018-0-57683000-1379285032_thumb.p

i5 4670k | Sapphire 7950 | Kingston 120GB SSD | Seagate 1TB | G.Skill Ripjaw X Series 8GB

PB238Q | Steelseries Sensei | Ducky DK9087 | Qck Heavy

Build Log: http://linustechtips.com/main/topic/44902-from-imac-to-my-own-creation/

Link to comment
https://linustechtips.com/topic/56810-c-programming-help/
Share on other sites

Link to post
Share on other sites

What are you going to be using this for?

It's college work. I can't figure out why my output isn't working.

i5 4670k | Sapphire 7950 | Kingston 120GB SSD | Seagate 1TB | G.Skill Ripjaw X Series 8GB

PB238Q | Steelseries Sensei | Ducky DK9087 | Qck Heavy

Build Log: http://linustechtips.com/main/topic/44902-from-imac-to-my-own-creation/

Link to comment
https://linustechtips.com/topic/56810-c-programming-help/#findComment-768141
Share on other sites

Link to post
Share on other sites

Well whats up with the scanf? it seems to have input| or something, not an array index. If you are only going to be working with one value I wouldn't even bother with an array index myself.

My rig: 2600k(4.2 GHz) w/ Cooler Master hyper 212+, Gigabyte Z68-UD3H-B3, Powercolor 7870 xt(1100/1500) w/AIO mod,

8GB DDR3 1600, 120GB Kingston HyperX 3K SSD, 1TB Seagate, Antec earthwatts 430, NZXT H2

Verified max overclock, just for kicks: http://valid.canardpc.com/show_oc.php?id=2609399

Link to comment
https://linustechtips.com/topic/56810-c-programming-help/#findComment-768145
Share on other sites

Link to post
Share on other sites

Well whats up with the scanf? it seems to have input| or something, not an array index. If you are only going to be working with one value I wouldn't even bother with an array index myself.

I thought char needed a limit on its input? So that's why I put 1 there :P

i5 4670k | Sapphire 7950 | Kingston 120GB SSD | Seagate 1TB | G.Skill Ripjaw X Series 8GB

PB238Q | Steelseries Sensei | Ducky DK9087 | Qck Heavy

Build Log: http://linustechtips.com/main/topic/44902-from-imac-to-my-own-creation/

Link to comment
https://linustechtips.com/topic/56810-c-programming-help/#findComment-768179
Share on other sites

Link to post
Share on other sites

Funny thing is, as I recall, C does nothing to babysit you. So even if you have an array there is no reason that you can't address a value outside the array. Or even negative values. But as you could imagine those are generally a very very bad ideas.

My rig: 2600k(4.2 GHz) w/ Cooler Master hyper 212+, Gigabyte Z68-UD3H-B3, Powercolor 7870 xt(1100/1500) w/AIO mod,

8GB DDR3 1600, 120GB Kingston HyperX 3K SSD, 1TB Seagate, Antec earthwatts 430, NZXT H2

Verified max overclock, just for kicks: http://valid.canardpc.com/show_oc.php?id=2609399

Link to comment
https://linustechtips.com/topic/56810-c-programming-help/#findComment-768212
Share on other sites

Link to post
Share on other sites

isn't the value stored in [0]

I've not coded in c for quite a while, but in a lot of other languages, you wouldn't need an array

[spoiler=»--((¯`·._.·¤~●oO му яιg Oo●~¤·._.·´¯))--«] Case: Corsair 600T White       CPU: Core i7 3930k @ 4.3GHz                     SSD: Crucial M500 480GB

MB:    Asus P9X79 LE         GPU: Nvidia GTX 780 Reference             HDD: WD Caviar Green 3TB

PSU:   Seasonic X-1050      RAM: G.Skill Trident X 32GB @ 2.4GHz   ☃ There are 10 types of people in this world. Those who understand binary and those who don't.

Link to comment
https://linustechtips.com/topic/56810-c-programming-help/#findComment-768217
Share on other sites

Link to post
Share on other sites

If you only want to grab a single character you don't need the array index on the input variable. The main problem is that you are reading input[1] this won't work because in C array indices start at 0 (so in this case input[1] will give you an undefined value as it is outside of the array)

 

You should also set the value of the input variable when you define it, otherwise it will be an undefined value until you set it to something (so there's a small chance it could be set to 'x' before you read anything).

Link to comment
https://linustechtips.com/topic/56810-c-programming-help/#findComment-768235
Share on other sites

Link to post
Share on other sites

@Flojer0, @Prince32780, @random314 

 

I changed all the input[1] to input and initialized input as 0, but well this happens:

 

Lab3$ ./switch

Please type a character [A-Z or a-z] ('x' to exit): a

 

 

x

Segmentation fault

 

 

I tried typing a then I kept hitting enter....then I hit x just to see what happened.

i5 4670k | Sapphire 7950 | Kingston 120GB SSD | Seagate 1TB | G.Skill Ripjaw X Series 8GB

PB238Q | Steelseries Sensei | Ducky DK9087 | Qck Heavy

Build Log: http://linustechtips.com/main/topic/44902-from-imac-to-my-own-creation/

Link to comment
https://linustechtips.com/topic/56810-c-programming-help/#findComment-768243
Share on other sites

Link to post
Share on other sites

Have you replaced all occurences on input[1] with just input?

 

Also you should put a space before the conversion specifier in scanf, because it won't strip leading whitespace from the standard input.

Yes I did. I still get nothing

i5 4670k | Sapphire 7950 | Kingston 120GB SSD | Seagate 1TB | G.Skill Ripjaw X Series 8GB

PB238Q | Steelseries Sensei | Ducky DK9087 | Qck Heavy

Build Log: http://linustechtips.com/main/topic/44902-from-imac-to-my-own-creation/

Link to comment
https://linustechtips.com/topic/56810-c-programming-help/#findComment-768316
Share on other sites

Link to post
Share on other sites

the | after input is unnecessary 

[spoiler=»--((¯`·._.·¤~●oO му яιg Oo●~¤·._.·´¯))--«] Case: Corsair 600T White       CPU: Core i7 3930k @ 4.3GHz                     SSD: Crucial M500 480GB

MB:    Asus P9X79 LE         GPU: Nvidia GTX 780 Reference             HDD: WD Caviar Green 3TB

PSU:   Seasonic X-1050      RAM: G.Skill Trident X 32GB @ 2.4GHz   ☃ There are 10 types of people in this world. Those who understand binary and those who don't.

Link to comment
https://linustechtips.com/topic/56810-c-programming-help/#findComment-768318
Share on other sites

Link to post
Share on other sites

you are missing a lot of quotes

[spoiler=»--((¯`·._.·¤~●oO му яιg Oo●~¤·._.·´¯))--«] Case: Corsair 600T White       CPU: Core i7 3930k @ 4.3GHz                     SSD: Crucial M500 480GB

MB:    Asus P9X79 LE         GPU: Nvidia GTX 780 Reference             HDD: WD Caviar Green 3TB

PSU:   Seasonic X-1050      RAM: G.Skill Trident X 32GB @ 2.4GHz   ☃ There are 10 types of people in this world. Those who understand binary and those who don't.

Link to comment
https://linustechtips.com/topic/56810-c-programming-help/#findComment-768328
Share on other sites

Link to post
Share on other sites

#include <stdio.h>int main (void){        char input;    input = 0;    while (input != 'x'){        printf("Please type a character [A-Z or a-z]");        printf(" ('x' to exit): ");        scanf( "%c \n", &input );//  Use switch statements to determine output        switch (input){            case 'x':            break;            case 'a' : case 'e' : case 'i' : case 'o' : case 'u' :            case 'A' : case 'E' : case 'I' : case 'O' : case 'U' :            printf("%s is a vowel\n", input);            break;            default:            printf("%s is a consonant\n", input);            break;        }    }}

this compiles, but I think something is missing

[spoiler=»--((¯`·._.·¤~●oO му яιg Oo●~¤·._.·´¯))--«] Case: Corsair 600T White       CPU: Core i7 3930k @ 4.3GHz                     SSD: Crucial M500 480GB

MB:    Asus P9X79 LE         GPU: Nvidia GTX 780 Reference             HDD: WD Caviar Green 3TB

PSU:   Seasonic X-1050      RAM: G.Skill Trident X 32GB @ 2.4GHz   ☃ There are 10 types of people in this world. Those who understand binary and those who don't.

Link to comment
https://linustechtips.com/topic/56810-c-programming-help/#findComment-768337
Share on other sites

Link to post
Share on other sites

the | after input is unnecessary 

That's not a pipe, it's just where I was last typing lol.

 

I'll see what you changed and see if it makes a difference.

 

You're missing a return 0; at the end lol. Besides that, that's exactly what I have :3

i5 4670k | Sapphire 7950 | Kingston 120GB SSD | Seagate 1TB | G.Skill Ripjaw X Series 8GB

PB238Q | Steelseries Sensei | Ducky DK9087 | Qck Heavy

Build Log: http://linustechtips.com/main/topic/44902-from-imac-to-my-own-creation/

Link to comment
https://linustechtips.com/topic/56810-c-programming-help/#findComment-768346
Share on other sites

Link to post
Share on other sites

Just noticed that the format specifiers at the bottom need to be %c not %s (%s is for a null terminated string of characters, so since it couldn't find a null it just keeps reading until it goes into some part of memory it shouldn't be in, which makes it generate a segmentation fault)

 

Like this:

#include <stdio.h>int main (void){        char input;    input = 0;    while (input != 'x'){        printf("Please type a character [A-Z or a-z]");        printf(" ('x' to exit): ");        scanf( " %c", &input );//  Use switch statements to determine output        switch (toupper(input)){            case 'x':            break;            case 'A' : case 'E' : case 'I' : case 'O' : case 'U' :            printf("%c is a vowel\n", input);            break;            default:            printf("%c is a consonant\n", input);            break;        }    }    return 0;}
Link to comment
https://linustechtips.com/topic/56810-c-programming-help/#findComment-768367
Share on other sites

Link to post
Share on other sites

why aren't you using pointers

[spoiler=»--((¯`·._.·¤~●oO му яιg Oo●~¤·._.·´¯))--«] Case: Corsair 600T White       CPU: Core i7 3930k @ 4.3GHz                     SSD: Crucial M500 480GB

MB:    Asus P9X79 LE         GPU: Nvidia GTX 780 Reference             HDD: WD Caviar Green 3TB

PSU:   Seasonic X-1050      RAM: G.Skill Trident X 32GB @ 2.4GHz   ☃ There are 10 types of people in this world. Those who understand binary and those who don't.

Link to comment
https://linustechtips.com/topic/56810-c-programming-help/#findComment-768368
Share on other sites

Link to post
Share on other sites

 

Just noticed that the format specifiers at the bottom need to be %c not %s (%s is for a null terminated string of characters, so since it couldn't find a null it just keeps reading until it goes into some part of memory it shouldn't be in, which makes it generate a segmentation fault)

I was actually playing around with that whilst I took the SS.

 

 

why aren't you using pointers

How?

 

 

Lab3$ ./switch

Please type a character [A-Z or a-z] ('x' to exit): a

s

a is a vowel

Please type a character [A-Z or a-z] ('x' to exit): s

s is a consonant

Please type a character [A-Z or a-z] ('x' to exit): d

s is a consonant

Please type a character [A-Z or a-z] ('x' to exit): c

d is a consonant

Please type a character [A-Z or a-z] ('x' to exit): f

c is a consonant

Please type a character [A-Z or a-z] ('x' to exit): ^C

Lab3$

 

^ what happens when I use all %c

i5 4670k | Sapphire 7950 | Kingston 120GB SSD | Seagate 1TB | G.Skill Ripjaw X Series 8GB

PB238Q | Steelseries Sensei | Ducky DK9087 | Qck Heavy

Build Log: http://linustechtips.com/main/topic/44902-from-imac-to-my-own-creation/

Link to comment
https://linustechtips.com/topic/56810-c-programming-help/#findComment-768390
Share on other sites

Link to post
Share on other sites

if this was java or c#, it'd be much easier :P

c likes to compare integers in switch statements

[spoiler=»--((¯`·._.·¤~●oO му яιg Oo●~¤·._.·´¯))--«] Case: Corsair 600T White       CPU: Core i7 3930k @ 4.3GHz                     SSD: Crucial M500 480GB

MB:    Asus P9X79 LE         GPU: Nvidia GTX 780 Reference             HDD: WD Caviar Green 3TB

PSU:   Seasonic X-1050      RAM: G.Skill Trident X 32GB @ 2.4GHz   ☃ There are 10 types of people in this world. Those who understand binary and those who don't.

Link to comment
https://linustechtips.com/topic/56810-c-programming-help/#findComment-768410
Share on other sites

Link to post
Share on other sites

The code I posted above shouldn't have a newline in the scanf statement I've fixed it now

Alright so can I not use default for all of the consonants? Because it prints is a consonant for every single one of them too.

 

 

Lab3$ ./switch

Please type a character [A-Z or a-z] ('x' to exit): a

a is a vowel

Please type a character [A-Z or a-z] ('x' to exit):

 is a consonant

Please type a character [A-Z or a-z] ('x' to exit): s

s is a consonant

Please type a character [A-Z or a-z] ('x' to exit):

 is a consonant

Please type a character [A-Z or a-z] ('x' to exit): d

d is a consonant

Please type a character [A-Z or a-z] ('x' to exit):

 is a consonant

Please type a character [A-Z or a-z] ('x' to exit):

 

i5 4670k | Sapphire 7950 | Kingston 120GB SSD | Seagate 1TB | G.Skill Ripjaw X Series 8GB

PB238Q | Steelseries Sensei | Ducky DK9087 | Qck Heavy

Build Log: http://linustechtips.com/main/topic/44902-from-imac-to-my-own-creation/

Link to comment
https://linustechtips.com/topic/56810-c-programming-help/#findComment-768418
Share on other sites

Link to post
Share on other sites

Alright so can I not use default for all of the consonants? Because it prints is a consonant for every single one of them too.

The scanf statement also needs a leading space.

 

Also you shouldn't use default for all the consonants anyway, what happens if what you type is neither a vowel nor a consonant?

Link to comment
https://linustechtips.com/topic/56810-c-programming-help/#findComment-768441
Share on other sites

Link to post
Share on other sites

The scanf statement also needs a leading space.

 

Also you shouldn't use default for all the consonants anyway, what happens if what you type is neither a vowel nor a consonant?

OMG THANK YOU IT'S FIXED!

 

Thanks all of you, I stare at these things all day and it's always the minute details :P

i5 4670k | Sapphire 7950 | Kingston 120GB SSD | Seagate 1TB | G.Skill Ripjaw X Series 8GB

PB238Q | Steelseries Sensei | Ducky DK9087 | Qck Heavy

Build Log: http://linustechtips.com/main/topic/44902-from-imac-to-my-own-creation/

Link to comment
https://linustechtips.com/topic/56810-c-programming-help/#findComment-768447
Share on other sites

Link to post
Share on other sites

3 is a consonant.

[spoiler=»--((¯`·._.·¤~●oO му яιg Oo●~¤·._.·´¯))--«] Case: Corsair 600T White       CPU: Core i7 3930k @ 4.3GHz                     SSD: Crucial M500 480GB

MB:    Asus P9X79 LE         GPU: Nvidia GTX 780 Reference             HDD: WD Caviar Green 3TB

PSU:   Seasonic X-1050      RAM: G.Skill Trident X 32GB @ 2.4GHz   ☃ There are 10 types of people in this world. Those who understand binary and those who don't.

Link to comment
https://linustechtips.com/topic/56810-c-programming-help/#findComment-768450
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×