My While loop doesn't give the value its supposed to give - c

We've been given a assignment at school and the
while loop at the end of the code doesn't seem to give the value I expected.
#include <stdio.h>
#include <string.h>
int main() {
char str[50],storage[50][50]={0},temp[50]={0};
printf("Input Formula without space:");
gets(str);
int x=0, y=0,z=0,w,i,top=-1,max=strlen(str);
for(i=0;i<max;i++){
printf("Eyyyyyy");
top+=1;
switch(str[top]){
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '-':
case '+':
case '*':
case '/':
case '9':storage[x][y]=str[top];x++;break;
case ')':
if (x-3<0)
break;
else{
while(storage[x-3][y]!=0){
temp[y]=storage[x-3][y];
y++;
printf("Hey");};
storage[x-3][y]='(';
while (temp[y]!=0){
storage[x-3][y+1]=temp[y];
y++;
};
for (z=2;z>=0;z--){
while (storage[x-z][y]!=0){
w=strlen(storage[x-3]);
storage[x-3][w]=storage[x-z][y];
}
}
}
x-=3;
break;
}
}
for(i=0;i<20;i++){
printf("%c",storage[0][i]);
}
for(i=0;i<20:i++){
printf("%c",storage[0][i]);
}
while(storage[x][y]!=0){
printf("%c",storage[0][y]);
y++;
}
}
I'd like to ask for some help on how do I make the while loop work. It doesn't give me any errors when compiling its just the value on while loop doesn't show up. only the other printfs before that.

regarding the while() statement at the end of the posted code:
while(storage[x][y]!=0){`
what are the initial values of x and y?
Since those values are already 'maxed out' the selection of storage[x][y] is past the end of the array `storage'
Suggest properly initializing those index values before entering the while() loop

Related

Can we call a 'Case' of Switch(); statement from anywhere else in the code [C Programming Language]

here is a demo code for explaining what I'm actually looking for
I want to call a 'case' of "switch" - statement from somewhere else in the code in C Language.
Here is the source code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
char a;
int main()
{
printf("Enter a number: ");
scanf("%c", &a);
switch(a){
case 'a':
printf("This is for A & C");
break;
case 'b':
printf("This is for only B");
break;
case 'c':
// Here i want to call "case 'a':"
goto case 'a';
// how can a call another case here?
break;
default:
printf("Default");
break;
}
getch();
return 0;
}
Thanks is Advance :D
You need the so-called "fall-through" case:
So instead of this:
case 'a':
printf("This is for A & C");
break;
case 'b':
printf("This is for only B");
break;
case 'c':
// Here i want to call "case 'a':"
Write this:
case 'a':
case 'c':
printf("This is for A or C");
break;
case 'b':
printf("This is for only B");
break;

C Program Takes Alphabetic Phones Numbers and Converts into Numberic Form

I'm working on a C code that will take an alphabetic phone number and convert into numbers. I'm stuck on the output. The output wil drop numbers needed. Such as 1-800-GATOR, will return 42867 but not 1-800-42867. This is what I have so far....
#include <ctype.h>
#include <stdio.h>
int main (void)
{
char ch;
int num_vowels = 0;
printf("Please Enter a phone number: ");
while ((ch = getchar()) != '\n')
{
switch (ch) {
case 'A': case 'B': case 'C':
putchar('2');
break;
}
switch (ch) {
case 'D': case 'E': case 'F':
putchar ('3');
break;
}
switch (ch) {
case 'G': case 'H': case 'I':
putchar ('4');
break;
}
switch (ch) {
case 'J': case 'K': case 'L':
putchar ('5');
break;
}
switch (ch) {
case 'M': case 'N': case 'O':
putchar ('6');
break;
}
switch (ch) {
case 'P': case 'Q': case 'R': case 'S':
putchar ('7');
break;
}
switch (ch) {
case 'T': case 'U': case 'V':
putchar ('8');
break;
}
switch (ch) {
case 'W': case 'X': case 'Y': case 'Z':
putchar ('9');
break;
}
printf("%c", num_vowels);
}
return 0;
}
Is it something that I'm missing in printf("%c", num_vowels);?
Any help is appricated!
You may have misunderstood the switch statement. While your usage does perform correctly, you only need one statement with multiple cases:
switch (ch) {
case 'A': case 'B': case 'C':
putchar('2');
break;
case 'D': case 'E': case 'F':
putchar('3');
break;
case 'G': case 'H': case 'I':
putchar('4');
break;
/* ... */
}
Note that nothing happens when the input doesn't match any of the cases. That's why only capital letters are transformed and sent to the output. One simple solution would be adding a default case. This way, when the input character is not any capital letter it is sent straight to output. Add this to the end of your switch:
switch(ch) {
case 'A': case 'B':
/* ... */
default:
putchar(ch);
break;
}
You could perform other checks as well, only printing certain characters for example.
As for printf("%c", num_vowels); there is nothing wrong with it, except that num_vowels is never used. So you should expect it to print a null character ('\0') after every character sent to output. I'm not sure about your intentions, but if it was a counter, you should increment the variable and print it only after reading input, outside the while loop.
#include <stdio.h>
#include <ctype.h>
int
main(int argc, char **argv)
{
char ch;
int num_letters = 0;
printf("Please Enter a phone number: ");
while ((ch = getchar()) != '\n')
{
if (isupper(ch)) num_letters++;
switch (ch) {
case 'A': case 'B': case 'C':
putchar('2');
break;
case 'D': case 'E': case 'F':
putchar('3');
break;
case 'G': case 'H': case 'I':
putchar('4');
break;
case 'J': case 'K': case 'L':
putchar('5');
break;
case 'M': case 'N': case 'O':
putchar('6');
break;
case 'P': case 'Q': case 'R': case 'S':
putchar('7');
break;
case 'T': case 'U': case 'V':
putchar('8');
break;
case 'W': case 'X': case 'Y': case 'Z':
putchar('9');
break;
default:
putchar(ch);
break;
}
}
printf("\n-- %d alphabetic letters used.\n", num_letters);
return 0;
}
Use int ch instead of char ch because getchar returns int. You should compare ch with EOF and handle that case.
Your code doesn't handle the case where a user enters a lower-case letter (e.g. 'a' instead of 'A'). You can correct this by using ch = toupper(ch);
You don't need to repeat each switch() statement.
num_vowels isn't being modified.
printf("%c", num_vowels) should be printf("%d") as num_vowels represents a human-readable number, not a character value.
printf(... should be after your while loop, not inside it.
You cannot use getchar and putchar to supplant user-input (i.e. disable local input echo), that requires platform-specific APIs. I recommend prompting the user to input a whole line of text first (into a char*), then process the line of text.
I guess you are wrong with how you are processing the input.
When you are taking input from the user i.e. 1-800-GATOR, you should skip the first 6 characters i.e "1-800-" and then start your switch case loop.
Also, the variable num_vowels is unchanged since you defined it till you print it. Why are you using it?
The code that will work for your statement will be:
#include <stdio.h>
int main (void)
{
char ch;
//int num_vowels = 0;
printf("Please Enter a phone number: ");
while ((ch = getchar()) != '\n')
{
switch (ch) {
case 'A': case 'B': case 'C':
putchar('2');
break;
case 'D': case 'E': case 'F':
putchar ('3');
break;
case 'G': case 'H': case 'I':
putchar ('4');
break;
case 'J': case 'K': case 'L':
putchar ('5');
break;
case 'M': case 'N': case 'O':
putchar ('6');
break;
case 'P': case 'Q': case 'R': case 'S':
putchar ('7');
break;
case 'T': case 'U': case 'V':
putchar ('8');
break;
case 'W': case 'X': case 'Y': case 'Z':
putchar ('9');
break;
}
}
printf("\n");
return 0;
}

switch case inside loop

I've run into a slight problem with my function. When I typed in 8 I want it to quit. However when I type 8, it prints out my default message then quits. What have I missed?
void Selection()
{
int selection;
while (selection != 8)
{
printMenu();
scanf("%d", &selection);
switch (selection)
{
case '1': /*FUNCTION HERE*/ ; break;
case '2': /*FUNCTION HERE*/ ; break;
case '3': /*FUNCTION HERE*/ ; break;
case '4': /*FUNCTION HERE*/ ; break;
case '5': /*FUNCTION HERE*/ ; break;
case '6': /*FUNCTION HERE*/ ; break;
case '7': /*FUNCTION HERE*/ ; break;
case '8': break;
default: printf("Unkown command please try again.\n"); break;
}
}
}
The line
scanf("%d", &selection);
inputs an int value, say 8. But in your case statement
case '8': break;
you are testing a character value. Please change all those case statements to such as
case 8: break;
Also, you must initialise the local variable selection before you first test it. Compiler should have warned you about that.

How to write continuously in an array

Im making a calculator for binary/hexadecimal numbers where the user insert an 8 digits hexadecimal number and i need to convert it to binary and print it following by the operations. My problem is that after i read the hexadecimal number in an array and try to convert it and store it in another array, after i print it i get weird caracters(V<#■   jst]tt)
This is part of my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <proyecto.h>
#include <string.h>
int main () {
char op,bin[31],hex[100],hex2[100];
int sizeh,repeat1,repeat2,n,z,i;
printf("Hexadecimal: ");
scanf("%s",hex);
convert(hex,bin,n);
convert function:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <proyecto.h>
#include <string.h>
void convert(char hex[],char bin[],int n){
int i,b;
printf("\nEquivalent binary value: ");
for(i=0;i<8;i++){
switch(hex[i]){
case '0': scanf("0000",bin); break;
case '1': scanf("0001",bin); break;
case '2': scanf("0010",bin); break;
case '3': scanf("0011",bin); break;
case '4': scanf("0100",bin); break;
case '5': scanf("0101",bin); break;
case '6': scanf("0110",bin); break;
case '7': scanf("0111",bin); break;
case '8': scanf("1000",bin); break;
case '9': scanf("1001",bin); break;
case 'A': scanf("1010",bin); break;
case 'B': scanf("1011",bin); break;
case 'C': scanf("1100",bin); break;
case 'D': scanf("1101",bin); break;
case 'E': scanf("1110",bin); break;
case 'F': scanf("1111",bin); break;
case 'a': scanf("1010",bin); break;
case 'b': scanf("1011",bin); break;
case 'c': scanf("1100",bin); break;
case 'd': scanf("1101",bin); break;
case 'e': scanf("1110",bin); break;
case 'f': scanf("1111",bin); break;
default: printf("\nInvalid hexadecimal digit %c ",hex[i]);
}
}
printf("%s",bin);
}
The point of the function is to write continously in the binary array the characters, so if i type AB the array will first insert 1010 and in the position 5 will keep writing 1011 but somehow i dont manage it to work
You can change scanf("0000",bin); to strcat(bin, "0000"); and so on.
Note that this method is cumbersome and bin must be large enough and initialized to the empty string, eg:
char bin[33] = "";

why does "type mismatch in redeclaration of hexadecimal" keep popping up?

I am a totally newbie about C programming. so my program is very long, sorry.
my professor wants to have a number system- binary to decimal, decimal to binary, octal to decimal, hexadecimal to binary. he also want to have a loop( if he wants to exit press [E], if not then press any key). Now i'm having a problem with this hexadecimal because it keeps saying " type mismatch in redeclaration" and i don't know now how to solve this problem.
so heres my not yet finished program because of "hexadecimal" problem. help me with this error. don't mind the octal to decimal, I am currently programming it :)
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 1000
long num, decimal(long), octal(long), binary(long),j;
char hexadecimal(char), k[MAX];
main()
{
char choice;
clrscr();
printf("[B]inary to Decimal\n");
printf("[D]ecimal to Binary\n");
printf("[O]ctal to Decimal\n");
printf("[H]exadecimal to Binary\n");
printf("[E]xit\n");
printf(" Enter your choice....");
choice=getche();
switch(choice)
{
case 'b':
case 'B': binary(j); break;
case 'd':
case 'D': decimal(num); break;
case 'o':
case 'O':
case 'h':
case 'H': hexadecimal(k[MAX]); break;
case 'e':
case 'E': return 0;
default: printf("\n Invalid choice.... press any key to REPEAT");
getch();
main();
}
printf("\nDo you want to [E]xit?");
choice=getch();
switch(choice)
{
case 'e':
case 'E': printf("\nInvalid choice... press any key to repeat");
getch();
main();
}
getch();
return 0;
}
long binary(long j)
{
long binary_val,decimal_val=0, base=1, rem;
printf("Enter a binary number( 1s & 0s): ");
scanf("%ld",&j);
binary_val=j;
while(j>0)
{
rem=j % 10;
decimal_val=decimal_val + rem * base;
j= j/ 10;
base=base * 2;
}
printf(" The Binary Number is %ld\n",binary_val);
printf(" Its decimal equivalent is = %d\n",decimal_val);
}
long decimal(long num)
{
long decimal_num, remainder, base=1, binary=0;
printf(" \nEnter a decimal integer: ");
scanf("%ld",&num);
decimal_num=num;
while(num>0)
{
remainder= num % 2;
binary=binary + remainder * base;
num=num/2;
base= base * 10;
}
printf(" Input number is %d\n",decimal_num);
printf(" Its binary equivalent is = %ld",binary);
}
char hexadecimal(char k[MAX])
{
long int i=0;
clrscr();
printf(" Enter any Hexadecimal number: ");
scanf("%s",&k);
printf("\n Equivalent binary value: ");
while(k[i])
{
switch(k[i])
{
case '0': printf("0000"); break;
case '1': printf("0001"); break;
case '2': printf("0010"); break;
case '3': printf("0011"); break;
case '4': printf("0100"); break;
case '5': printf("0101"); break;
case '6': printf("0110"); break;
case '7': printf("0111"); break;
case '8': printf("1000"); break;
case '9': printf("1001"); break;
case 'a':
case 'A': printf("1010"); break;
case 'b':
case 'B': printf("1011"); break;
case 'c':
case 'C': printf("1100"); break;
case 'd':
case 'D': printf("1101"); break;
case 'e':
case 'E': printf("1110"); break;
case 'f':
case 'F': printf("1111"); break;
default: printf("\n Invalid hexadecimal digit %c",k[i]); return 0;
}
i++;
}
}
The error you are getting type mismatch in redeclaration of hexadecimalis a result of the difference between the function you prototyped and implemented.
Your prototype is:
char hexadecimal(char), k[MAX];
This line prototypes a function hexadecimal that returns a char and takes a char as an argument AND this line also declares a global char array k of size MAX.
Your actual function is:
char hexadecimal(char k[MAX])
This function is a function that returns a char, but instead of taking a char like your prototype it instead takes a char array of size MAX. As you can see the prototyped function and the function itself are not the same. By making the functions exactly the same you will fix your issue.
To be honest, you don't need to pass anything into that function nor make a global char array as you can locally hold the array based on your code. The only other time you use the array you just pass it to this function which means it is better of as a local to that function anyway. So, you can simply do this:
char hexadecimal(void)
{
char k[MAX]
//same code below...
Now the function takes no arguments and k is still declared in the function, but is local instead of global. The prototype for this function would simply be:
char hexadecimal(void);

Resources