I'm learning starting to learn C, so I tried to make a simple program with what I know. The program SHOULD ask for a letter, ask if you want the position of a letter in the alphabet(if so it displays "x is the y letter in the alphabet" with the suffix) and then it asks if you want the letter in binary and puts it in binary. But for some reason it doesn't work. I tried "manual debugging"(using puts() to see what doesn't work) and the class "getNumber" works but the value changes when it goes to the main class.
a returns "2686719" instead of "a is the 1st letter in the alphabet" and b returns "2b is the 2z\<<y in the alphabet".
This is the code, what did I do wrong?
#include <stdio.h>
#include <stdlib.h>
//011
int main(){
int x = 2;
char letter;
puts("Write a letter");
char temp2;
scanf("%c", &temp2);
int number;
letter = temp2;
puts("get alphabet position? y/n ");
char temp[2];
scanf("%s", temp);
char answer = temp[0];
int tempNumber2 = getNumber(letter);
if(answer == 'y'){
char suffix[3];
int number = tempNumber2;
printf("%d", number);
if (number == 1){
x = 1;
suffix[0] = "s";
suffix[1] = "t";
}else if (number < 27){
suffix[0] = "t";
suffix[1] = "h";
x = 1;
}
if (x == 1){
printf("%c is the %d", letter, number);
printf("%s in the alphabet\n", suffix);
}}else if (answer == 'n'){
x = 1;
}else if (answer != 'y' && answer != 'n'){
puts("ERROR, invalid letter");
x = 3;
};
if(x != 3){
puts("Convert to binary?");
}
return 0;
}
int getNumber(letter){
char tempLetter = letter;
int tempNumber = -1;
char alphabet[27] = "abcdefghijklmnopqrstuvwxyz";
int i = 0;
while(i < 27){
i++;
if(tempLetter == alphabet[i]){
tempNumber = i;
tempNumber++;
printf("%d \n", tempNumber);
return tempNumber;
}
};
}
By the way, I haven't gotten to the binary part hence why it's empty.
As everyone stated in the comments your problem is that suffix's last char is not set to '\0'.
Moreover, you could getNumber in a much simpler way:
int getNumber(char letter) {
return (int)(letter - 'a') + 1;
}
Btw, you can make your code look a little better (didn't change it too much):
int main() {
char letter;
char answer;
puts("Write a letter");
scanf("%c", &letter);
getchar(); // read \n
puts("get alphabet position? y/n ");
scanf("%c", &answer);
getchar(); // read \n
if (answer == 'n') {
return 0;
}
if (answer != 'y') {
puts("ERROR, invalid letter");
return 1;
}
int number = getNumber(letter);
char suffix[3];
suffix[2] = 0;
if (number == 1) {
suffix[0] = 's';
suffix[1] = 't';
} else if (number == 2) {
suffix[0] = 'n';
suffix[1] = 'd';
} else if (number == 3) {
suffix[0] = 'r';
suffix[1] = 'd';
} else {
suffix[0] = 't';
suffix[1] = 'h';
}
printf("%c is the %d%s letter in the alphabet\n", letter, number, suffix);
puts("Convert to binary?");
return 0;
}
Related
#include <stdio.h>
int main(void)
{
int a;
scanf("%d", &a);
for (int i = 0; ; i++)
{
char values[2];
scanf("%c", &values[0]);
scanf("%d", &values[1]);
if (values[0] == '*')
{
a = a * values[1];
printf(" = %d\n", a);
}
else if (values[0] == '+')
{
a = a + values[1];
printf(" = %d\n", a);
}
else if (values[0] == '%')
{
a = a % values[1];
break;
}
}
printf("%d", a);
}
When I input 5 + 3 + 7 + 10 + 2 + 3 + 1 % 11, it would show 5 (because 5%11 = 5). But the + operation didn't work. Can you see what is the problem here?
I think as values[2] is only two variable you need, you can use two different variable to do your job. use one char type variable and one int type variable as you need these two. You have another problem in your code, use a getchar() in the inside of the loop then your code will work fine, cause when you give a integer value as input then take a character value last you enter the new line that goes to that character that is why your code was giving error.
#include <stdio.h>
int main(void){
int a;
scanf("%d", &a);
for (int i = 0; ; i++)
{
getchar();
char ch;
int value;
scanf("%c", &ch);
scanf("%d", &value);
if (ch == '*')
{
a = a * value;
printf(" = %d\n", a);
}
else if (ch == '+')
{
a = a + value;
printf(" = %d\n", a);
}
else if (ch == '%')
{
a = a % value;
break;
}
}
printf("%d", a);
}
There are multiple problems:
value is an array of char: scanning the operator into a char is fine, but the value should be converted into an int variable.
the scanf("%c", ...) will store the next byte into the variable, but after converting an int, the next byte is the pending space or newline, not the '+'. You should use scanf(" %c", ...) to skip the whitespace after the previous conversion.
Here is a modified version:
#include <stdio.h>
int main(void) {
int a;
if (scanf("%d", &a) != 1)
return 1;
for (int i = 0; ; i++) {
char op;
int value;
if (scanf(" %c", &op) != 1)
break;
if (scanf("%d", &value) != 1)
break;
if (op == '*') {
a = a * value;
printf(" = %d\n", a);
} else
if (op == '+') {
a = a + value;
printf(" = %d\n", a);
} else
if (op == '-') {
a = a - value;
printf(" = %d\n", a);
} else
if (op == '%') {
a = a % value;
printf(" = %d\n", a);
} else
if (op == '/') {
a = a / value;
printf(" = %d\n", a);
} else {
printf("invalid operator %c\n", op);
break;
}
}
printf("%d\n", a);
return 0;
}
This is my code. The second printf and getchar does not pop up after hitting enter for the first scanf. I am unsure why the second printf and getchar are not working and what the fix would be.
#include <stdio.h>
int main()
{
int choice;
int i;
i = 0;
int n;
n = 1;
int p;
char message[1000];
printf("Would you like to (1) encrypt or (2) decrypt?: ");
scanf("%i\n", choice);
printf("\nType your message: ");
message[p] = getchar();
for (i = 1; (i < 1000 && message[p] != '\0');) {
message[p] = message[p] + n;
n + 1;
}
for (i = 0; (i < 1000 && message[p] != '\0');) {
message[p] = message[p] - n;
n + 1;
}
return 0;
}
Your encription method works for encription and decription. This compiles and takes in a string including spaces modifies it and prints it out.
#include <stdio.h>
int main() {
int choice;
char temp;
int i = 0;
int n = 1;
int p, b;
char message[1000];
char endmessage[1000];
printf("Would you like to (1) encrypt or (2) decrypt?: ");
scanf("%i", &choice); // change as per comment above
scanf("%c", &temp); /* temp statement to clear buffer otherwise a second character has to be typed before the next print statement is executed */
printf("Type your message: "); //prompt added for user clarity
scanf("%[^\n]", message); /* this scanf line allows for spaces in the message */
printf("\nmessage typed is: %s \n", message);
p = (sizeof( message) + 1); /* strings need to have an ending '\0'
message[p] = '\0';
if (choice == 1) {
for (i = 0; (i < 1000 && message[i] != '\0');) {
endmessage[i] = (message[i] + n);
i +=1;
}
b = (sizeof( endmessage)); // replace encripted \0 with '\0'
endmessage[b] = putchar('\0');
printf("\nYour message encripted is: %s\n\n", endmessage);
}
if (choice == 2) {
for (i = 0; (i < 1000 && message[i] != '\0');) {
endmessage[i] = message[i] - n;
i += 1;
}
b = (sizeof(endmessage) -2); // replace decripted \0 with '\0'
endmessage[b] = putchar('\0');
printf("\nYour message decripted is: %s\n\n", endmessage);
}
}
I made a hangman game using a while loop. I want to use the number of dashes == 0 to break the loop so the player knows that they won. The word is hidden behind "-"s. My word is coding, so it has 6 dashes.
When I run the program, the first guess at the word is an accurate number of dashes. But after the second guess, and so on, it is adding all of the dashes from all the guesses together, not just from that one turn. By the third turn it says I have 9 dashes because that's how many is left over in all of the turns. Any ideas on how to fix?
char word[] = { "Coding" };
char dash[] = {"------"};
char guess;
int numguesses = 10;
int count;
int loopcount;
int i;
int dashcount;
int main()
{
printf("Your Word Is: %s", &dash);
//printf("\nWord: %c", &dash);
loopcount = 10;
count = 1;
while (count <= loopcount)
{
printf("\n\nPick a letter to guess: ");
scanf(" %c", &guess);
printf("\n\nYour Guess Was %c. ", guess);
count = count + 1;
if (guess == 'c')
{
dash[0] = 'c';
printf("\n\n %s", dash);
}
else if (guess == 'o')
{
dash[1] = 'o';
printf("\n\n %s", dash);
}
else if (guess == 'd')
{
dash[2] = 'd';
printf("\n\n %s", dash);
}
else if (guess == 'i')
{
dash[3] = 'i';
printf("\n\n %s", dash);
}
else if (guess == 'n')
{
dash[4] = 'n';
printf("\n\n %s", dash);
}
else if (guess == 'g')
{
dash[5] = 'g';
printf("\n\n %s", dash);
}
else
printf("That Letter Is Not Apart of this Word");
printf("\n\nYou are at Guess Number %d, Your word is at %s", count, dash);
for (i = 0; dash[i]; i++)
{
if (dash[i] == '-')
{
dashcount++;
}
}
printf("\n\nDoes it work ?: %d", dashcount);
}
if (count < numguesses)
{
printf("\n\n\nYou Won the Game, Your Awesome!!");
}
else
{
printf("\n\n\nI'm Sorry, the word was %s.", word);
}
printf("\n\n");
return 0;
}
The issue is that you are not resetting the dashcount variable, so every time it starts with previous value and adds the dashes left
for (i = 0; dash[i]; i++)
{
if (dash[i] == '-')
{
dashcount++;
}
}
You can easily fix this in this way
dashcount=0;
for (i = 0; dash[i]; i++)
{
if (dash[i] == '-')
{
dashcount++;
}
}
I am confused, when i use array values to assign another array values. the original array deletes the values used
int main(int argc, char *argv[]) {
char original[ORIGINAL_SIZE];
int isbn[ISBN_SIZE];
int index = 0;
int code;
int weight = 10;
int weightedValue;
int weightedSum = 0;
printf("Enter an ISBN to validate: ");
validateISBNArray(original);
while(index < ORIGINAL_SIZE){
if(original[index] != '-'){
if(original[index] == 'x' || original[index] == 'X') isbn[index] = 10;
else if(original[index] == 0) isbn[index] = 0;
else isbn[index] = original[index]-48;
code = isbn[index];
//printf("%d", code);
weightedValue= code*weight;
weight--;
weightedSum += weightedValue;
}
index++;
}
printf("%s",original);
if(weightedSum%11==0) printf("The ISBN %s, is VALID", original);
else printf("The ISBN %s, is NOT VALID", original);
return 0;
}
validateISBNArray has no effect on original array
this is the code for the not affecting function
void validateISBNArray(char array[]){
int index = 0;
int countDigits = 0;
int value = 0;
clearArray(array, ORIGINAL_SIZE);
scanf("%s",array);
while(index < ORIGINAL_SIZE){
//printf("%d %c %d\n", index, array[index], countDigits);
if(((array[index]-48) >= 0 && (array[index]-48) <= 9) || (array[index] == 'x'|| array[index] == 'X')) {
countDigits++;
index++;
}
else if(array[index] == '-' || array[index] == 0) index++;
else{
printf("INVALID CHARACTER %d = %c. Please Enter Digits Or/And Hyphens Only: ", index, array[index]);
index = 0;
countDigits = 0;
clearArray(array, ORIGINAL_SIZE);
scanf("%s",array);
}
if(index == ORIGINAL_SIZE && countDigits != 10){
printf("INVALID NUMBER OF DIGITS %d. Please Enter 10 Digits: ", countDigits);
index = 0;
countDigits = 0;
clearArray(array, ORIGINAL_SIZE);
scanf("%s",array);
}
}
//printf("%s", array);
}
Ok I fixed it removing the need for the isbn array
int main(int argc, char *argv[]) {
char original[ORIGINAL_SIZE];
int isbn[ISBN_SIZE];
int index = 0;
int code;
int weight = 10;
int weightedValue;
int weightedSum = 0;
printf("Enter an ISBN to validate: ");
validateISBNArray(original);
while(index < ORIGINAL_SIZE){
if(original[index] != '-'){
if(original[index] == 'x' || original[index] == 'X') code = 10;
else if(original[index] == 0) code = 0;
else code = original[index]-48;
weightedValue = code*weight;
weight--;
weightedSum += weightedValue;
}
index++;
}
if(weightedSum%11==0) printf("The ISBN %s, is VALID", original);
else printf("The ISBN %s, is NOT VALID", original);
return 0;
}
I have a school assignment to make a hangman game. The game works how I want it to except for one small glitch. If the user entered word is 4 letters or less, the hidden word is displayed with an extra "_\377" at the end. When the user entered word is 5 letters or more, then there is no glitch. I am hoping that someone would be kind enough to help me trouble shoot the problem. Thanks in advance!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int letterfinder(char string[], char a, int vari)
{
int length = strlen(string);
int i = vari;
int val = 0;
while( i <= length && val != 1)
{
if( string[i] == a)
{
val = 1;
}
i++;
}
if( val == 0)
{
return 100;
}
else
{
return i;
}
}
int main()
{
char inWord[] = "1111111111111111111111111111";
char outWord2[] = "1111111111111111111111111111";
char guess;
int gameover = 0;
int trys = 10;
int vari = 0;
printf("Please enter a word: ");
gets(inWord);
printf("%s\n", inWord);
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
int i2 = 0;
int j2 = 0;
int i3 = 0;
i2 = strcspn(inWord, outWord2);
char outWord[80];
while(i3 < i2)
{
outWord[i3] = '1';
i3++;
}
while(j2 < i2)
{
outWord[j2] = '-';
j2++;
}
puts(outWord);
while(gameover != 1 )
{
printf("What is your guess: ");
scanf("%s", &guess);
vari = 0;
if(letterfinder(inWord, guess, vari) == 100)
{
printf("Wrong!");
trys--;
printf("You have %d attempts left\n", trys);
if(trys == 0)
{
gameover = 1;
printf("You ran out of attempts. Game over\n");
}
}
else
{
outWord[(letterfinder(inWord, guess, vari) - 1)] = guess;
vari = (letterfinder(inWord, guess, vari));
while(letterfinder(inWord, guess, vari) != 100)
{
outWord[(letterfinder(inWord, guess, vari) - 1)] = guess;
vari = letterfinder(inWord, guess, vari);
}
puts(outWord);
}
int value = 0;
i3 = 0;
while( i3 <= i2)
{
if( outWord[i3] == '-')
{
value = 1;
}
i3++;
}
if(value != 1)
{
printf("Congratulations, you have guessed the word!\n");
gameover = 1;
}
}
return 0;
}
Your code has Undefined Behaviour. In the cases it "works" it is only by chance/luck. char guess; scanf("%s", &guess); That causes memory corruption as you are writing a string to a variable that can only hold a single char. Even a single letter guess will require two characters to store as all C strings are NUL terminated.
– kaylum