Jump to content

I keep getting a segmentation fault or infinite loop...I believe there is something wrong with the scanf statement or something. I need to my while loop to cancel when 'n' is inputted and when y or any other char is input, it keeps going. I've been at this all day and am going to put my head through the wall momentarily. Please help.

 

post-33018-0-09776200-1382227683_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/67213-c-programming-help/
Share on other sites

Link to post
Share on other sites

I want to come to your house, touch your head and absorb your knowledge on C. I'm still trying to learn python, although I bet if I sat down for a couple of hours I could understand it.

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/67213-c-programming-help/#findComment-919578
Share on other sites

Link to post
Share on other sites

I want to come to your house, touch your head and absorb your knowledge on C. I'm still trying to learn python, although I bet if I sat down for a couple of hours I could understand it.

I say avoid it at all costs. I had a fairly simple time with python but C has been nothing but trouble for me. I think it has more to do with the teacher of C that i have because he's useless whereas my Python teacher actually taught you.

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/67213-c-programming-help/#findComment-919599
Share on other sites

Link to post
Share on other sites

end and endShuffle should not be arrays since they are only one char. And even if they are arrays you cannot access position 1 when the array only has 1 position because they start at 0.

 

With this code it works for me:

#include <stdio.h>#include <stdlib.h>#include <string.h>int main() {  int i, j;  char string[81], end = 'y', endShuffle = 'y', temp;  srand(0);  while(end != 'n') {    endShuffle = 'y';    printf("Please enter a string with 10-80 characters:\n");    scanf(" %s", string);    printf("Original string: %s\n", string);    while(endShuffle != 'n') {      for(i = strlen(string) - 1; i >= 1; i--) {        j = (int) rand() % i;        temp = string[i];        string[i] = string[j];        string[j] = temp;      }      printf("New string: %s\n", string);      printf("Would you like to shuffle this string again? ");      scanf(" %c", &endShuffle);    }    printf("Would you like to shuffle another string? ");    scanf(" %c", &end);  }  return 0;}

PS: Please, next time use the code tag (<> in the editor) to make it easier for everyone, even if you want to put an image of the error or something else

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

Link to post
Share on other sites

end and endShuffle should not be arrays since they are only one char. And even if they are arrays you cannot access position 1 when the array only has 1 position because they start at 0.

 

With this code it works for me:

PS: Please, next time use the code tag (<> in the editor) to make it easier for everyone, even if you want to put an image of the error or something else

I can't copy and paste because I have to work on the linux server of my college from home. I've wanted to do this. I guess I'll just type it all next time or something. When I don't make them arrays, I get a warning of making a comparison between a pointer and an integer.

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/67213-c-programming-help/#findComment-919618
Share on other sites

Link to post
Share on other sites

I can't copy and paste because I have to work on the linux server of my college from home. I've wanted to do this. I guess I'll just type it all next time or something. When I don't make them arrays, I get a warning of making a comparison between a pointer and an integer.

How are you connecting? Through SSH? If so you can open the code in Vi on the terminal and copy from there (I don't think you can copy everything at once though!) or try to forward the graphical server and open gedit from the server (if it is allowed).

 

Anyways, that error might be if you use scanf of a char like scanf("%c", end). Scanf expects a pointer as the second argument so you must do scanf("%c", &end), like I did above.

Edit: As they are chars, in the ifs you must compare (end != 'c') instead of (end != "n"), because 'n' is a char and "n" is a string.

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

Link to post
Share on other sites

How are you connecting? Through SSH? If so you can open the code in Vi on the terminal and copy from there (I don't think you can copy everything at once though!) or try to forward the graphical server and open gedit from the server (if it is allowed).

 

Anyways, that error might be if you use scanf of a char like scanf("%c", end). Scanf expects a pointer as the second argument so you must do scanf("%c", &end), like I did above.

Edit: As they are chars, in the ifs you must compare (end != "c") instead of (end != "n"), because 'n' is a char and "n" is a string.

I think you dun fixed it again for me MikeD! How can I ever repay you? :D

 

Also, I use SSH through Putty to connect.

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/67213-c-programming-help/#findComment-919664
Share on other sites

Link to post
Share on other sites

I think you dun fixed it again for me MikeD! How can I ever repay you? :D

 

Also, I use SSH through Putty to connect.

 

If you're using Putty you can pair it with WinSCP to be able to browse the server file structure and to open and edit files. I still like Vi better!

 

You are welcome! I'm just glad to be of assistance!

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

Link to post
Share on other sites

If you're using Putty you can pair it with WinSCP to be able to browse the server file structure and to open and edit files. I still like Vi better!

 

You are welcome! I'm just glad to be of assistance!

You da man MikeD! I'll look into those next time. I think the next two programs I have to do this weekend may be the last bit of code I do in C for a long time because I'm onto C++ soon. Thanks for the help!

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/67213-c-programming-help/#findComment-919702
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

×