Vigenere cipher in cs50 Segfau [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am not experienced coder.Please dont judge for bad styling.I've run into an issue with the vingenere cipher
It keeps giving me segfau
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>**
int main(int argc,string argv[])
{
while (argc!=2)
{
printf("Incorrect input\n");
return 1;
}
printf("plaintext:");
string plaintext=get_string();
string num=argv[1];
int keylength=strlen(num);
for (int j=0, n=strlen(plaintext); j<n; j++)
{
char letter=plaintext[j];
if (isalpha(letter))
{
This is the reason of Segmantation Fault error.
If i don't check if num is upper or lower case,
code runs normally.
if (isupper(letter))
{
if (isupper(num))
{
printf ("%c",((((letter-65)+(num[j%keylength]-65))%26)+65));
}
else
{
printf ("%c",((((letter-65)+(num[j%keylength]-97))%26)+65));
}
}
else
{
if (isupper(num))
{
printf ("%c",((((letter-97)+(num[j%keylength]-65))%26)+97));
}
else
{
printf ("%c",((((letter-97)+(num[j%keylength]-97))%26)+97));
}
}
}
else
{
printf ("%c",letter);
}
}
printf("\n");
return 0;
}

Most likely, you meant
if (isupper(num[j % keylength))
Passing a string (really a char *) to isupper() is an undefined operation, and may produce weird effects depending on how isupper() is implemented. The GNU implementation ---
#define __ctype_lookup(__c) ((__ctype_ptr__+sizeof(""[__c]))[(int)(__c)])
#define isupper(__c) ((__ctype_lookup(__c)&(_U|_L))==_U)
--- uses a lookup table with all sorts of fun pointer math and casting under the hood, so it likely allows a char * to be passed without the compiler complaining, but it's definitely not going to produce good results. Change your if statement, and it should work better.

Related

Subtraction doesn't work in do while how can I fix it? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Hi I am new in c and I making a text adventure game. But I have a problem withe subtraction in a do while loop. The problem is that the number doesn't change it remains at 95 or in 90 in the second statement. Can someone help how to fix that and explain to me how subtraction works in a loop? Also, I want the loop end when the enemy's life is at zero
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
struct batman{
int helath,punch,kick,darts;
};
int main(void) {
int s=0,sum=0,r,knife=5,gun1=10,punch,enemy=100;
struct batman b;
b.helath=100;
b.punch=5;
b.kick=10;
b.darts=100;
printf("\nPress 1 for punch and 2 for kick");
srand(time(NULL));
do{
scanf("%d",&punch);
if(punch==1){
sum=b.punch-enemy;
printf("\nEnemy's Helath %d",sum);
}
else if(punch==2){
sum=b.kick-enemy;
printf("\nEnemy's Helath %d",sum);
}
r=rand()%2;
if(r==1){
s=b.helath-knife;
printf("\nBatman's Health%d",s);
}
else if(r==2){
s=b.helath-gun1;
printf("\nBatman's Health%d",s);
}
}while(punch==1||punch>=2);
return 0;
}
The value of enemy never changes, so unless the value of punch changes this is effectively a constant. You probably want to do something like:
enemy -= punch;
if (enemy <= 0) {
printf("Batman wins!");
}
to update the health of the enemy. For batman you probably want to do the same, i.e.,
b.health -= gun;
if (b.health <= 0) {
printf("Oh no! Batman lost.");
} else {
printf("Batman's health: %d", b.health);
}
Your game should probably end when either batman.health or enemy reaches 0, so you would want to add a check.

How do I resolve this int overflow? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
int frequency(string note)
{
int i;
float f;
int n=0;
float octave= note[strlen(note)-1];
if(strlen(note)==3)
{
if(note[1]=='#')
{
n+=1;
}
else if(note[1]=='b')
{
n-=1;
}
}
if(note[0]=='B')
{
n+=2;
}
else if(note[0]=='C')
{
n-=9;
}
else if(note[0]=='D')
{
n-=7;
}
else if(note[0]=='E')
{
n-=5;
}
else if(note[0]=='F')
{
n-=4;
}
else if(note[0]=='G')
{
n-=2;
}
n+=(octave-4.0)*12.0;
float p= n/12.0;
f=(int)(round(pow(2.0,p)*440.0));
return f;
}
So basically whenever I run this code I get an error stating "runtime error: value 7.3641e+16 is outside the range of representable values of type 'int'"
Then the value returned is just-2147483648. I've looked it up online and haven't found an answer that helps me with my code. Also this was made in the cs50 IDE so there are a bunch of commands and things that are imported. My program compiles properly and it can run so how do I fix this?
Frequencies of notes of the chromatic scale are not integers in the first place.
If you are passed a two character string like Cb, you get a stupidly huge octive with a frequency way too high.

Compiler doesn't show any errors or warnings but the program doesn't work [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I tried to build and run the following program, but it breaks down executing. I thought maybe I made a mistake but 0 errors and 0 warnings were shown.
After researching such behavior on stackoverflow, I mostly saw some misplaced semicolons or forgotten address-operators, which I do not see in this source code or am I overlooking something?
Could some C or GCC Guru tell me what is wrong and why?
Operating system is Windows 7, and compiler had enabled:
-pedantic -w -Wextra -Wall -ansi
Here is the source code:
#include <stdio.h>
#include <string.h>
char *split(char * wort, char c)
{
int i = 0;
while (wort[i] != c && wort[i] != '\0') {
++i;
}
if (wort[i] == c) {
wort[i] = '\0';
return &wort[i+1];
} else {
return NULL;
}
}
int main()
{
char *in = "Some text here";
char *rest;
rest = split(in,' ');
if (rest == NULL) {
printf("\nString could not be devided!");
return 1;
}
printf("\nErster Teil: ");
puts(in);
printf("\nRest: ");
puts(rest);
return 0;
}
The expected behavior is that the string "Some text here" is split at its first space ' ' and the expected output would be:
Erster Teil: Some
Rest: text here
You are modifying a string literal, that's undefined behavior. Change this
char* in = "Some text here";
to
char in[] = "Some text here";
This makes in an array and initializes it with "Some text here". You should use const to prevent accidentally having this bug when you define a pointer to a string literal.

I am getting runtime error (SIGSEGV) in a c program [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Why I am getting runtime error (SIGSEGV) on the following code:
#include<stdio.h>
#include<string.h>
int main()
{
int t_line,count[10000],i;
scanf("%d",&t_line);
for(i=1;i<=t_line;i++)
{
fflush(stdin);
gets(t);
count[i]=(int)t[0]+(int)t[1]+(int)t[2];
}
for(i=1;i<=t_line;i++)
printf("%d\n",count[i]);
return 0;
}
I have also tried to solve this problem by initialized all the elements of array.
I am wondering how the code compiled, without declaration of the variable t. But, still the only missing elemnt was: char t[your choice of size];. Apart from that
#include<stdio.h>
//#include<string.h> No need of this header,a s you are not using any string functions
int main()
{
int t_line,count[10000],i;
char t[64];//you need to declare the variable before using it
scanf("%d",&t_line);
//Its safer if you check this
if(t_line >= 10000)//if you use 0 and < t_line in for loop below then change the condition to: if(t_line > 10000)
{
printf("ERROR: Limit exceeded. Not enough memory.\n");
return 1;//or you could use exit(1); and #include <stdlib.h>
}
for(i=1;i<=t_line;i++)//suggested: for(i=0;i<t_line;i++)
{
//fflush(stdin);
//gets(t);
char *rc = fgets(t, sizeof(t), stdin);
if(rc != NULL)
{ t[strlen(t) - 1] = '\0';\\because fgets gets the \n into the string too. This line makes fgets similar to gets, improving safety from overflow.
}
else
{
// handle fgets failed error
}
count[i]=(int)t[0]+(int)t[1]+(int)t[2];
}
for(i=1;i<=t_line;i++)//suggested: for(i=0;i<t_line;i++)
printf("%d\n",count[i]);
return 0;
}
Find the solution and suggested changes inline as code comments.
In C, its better to use indexes starting from 0, unless there is a specific requirement to use other values.

Using a function pointer [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have a function in C that is crashing my code and I'm having a hard time figuring out what is going on. I have a function that looks like this:
#define cond int
void Enqueue(cond (*cond_func)());
cond read() {
return IsEmpty(some_global); // Returns a 1 or a 0 as an int
}
Enqueue(&read);
However, when running the above, it segfaults as soon as Enqueue is called. It doesn't even execute anything inside the function. I ran gdb and it just shows it dying as soon as Enqueue is called- no statements are processed within it. Any idea what is going on? Any help would be appreciated.
Can you give more information about the code because according to my interpretation the code is working fine.I have tried this-
#define cond int
void Enqueue(cond (*cond_func)());
cond read()
{
int some_global=1;
return IsEmpty(some_global); // Returns a 1 or a 0 as an int
}
int IsEmpty()
{
return 1;
}
void Enqueue(cond (*cond_func)())
{
printf("Perfect");
return 0;
}
int main()
{
Enqueue(&read);
return 0;
}
And it is working fine.
#define cond int
was meant to be:
typedef int cond;
Although defining an alias for your function pointer might be much more reasonable here, for example:
typedef int (*FncPtr)(void);
int read() {
printf("reading...");
}
void foo(FncPtr f) {
(*f)();
}
int main() {
foo(read);
return 0;
}
This works fine:
#include <stdio.h>
#include <stdbool.h>
typedef bool cond;
void Enqueue(cond (*cond_func)(void)) {
printf("In Enqueue()...\n");
cond status = cond_func();
printf("In Enqueue, 'status' is %s\n", status ? "true" : "false");
}
bool IsEmpty(const int n) {
return true;
}
cond my_cond_func(void) {
printf("In my_cond_func()...\n");
return IsEmpty(1);
}
int main(void) {
Enqueue(my_cond_func);
return 0;
}
Your problem is likely coming from somewhere else, such as your definition of Enqueue() which you don't provide, or the fact your function is called read() and is conflicting with the more common function of that name.

Resources