I'm practicing with this book "C programming - a modern approach 2" and the programming project 11 of chapter 8 says as follows:
Modify Programming Project 4 from chapter 7 (I'm going to attach the
code below) so that the program labels its output:
Enter phone number: 1-800-COL-LECT
In numeric form: 1-800-265-5328
The program will need to store the number (either in its original form
or in its numeric form) in an array of character until it can be
printed. You may assume that the phone number is no more than 15
characters long
The thing that i don't get is the meaning of the part in bold ("so that the program labels its output"). What it means? I can understand English quite well but in this case I really don't get what the Author is talking about.
I already googled the meaning of "labeling" in CS and it talk about goto ect... This chapter is about Arrays so i guess the second part is important and is actually the answer and in fact is not a problem for me to modify the program as requested, I just would like to really understand the whole request and what I'm doing.
The code of the program to modify:
#include <stdio.h>
int main(void)
{
char ch;
printf("Enter phone number: ");
while ((ch = getchar()) != '\n') {
switch (ch) {
case 'A': case 'a': case 'B': case 'b': case 'C': case 'c':
printf("2");
break;
case 'D': case 'd': case 'E': case 'e': case 'F': case 'f':
printf("3");
break;
case 'G': case 'g': case 'H': case 'h': case 'I': case 'i':
printf("4");
break;
case 'J': case 'j': case 'K': case 'k': case 'L': case 'l':
printf("5");
break;
case 'M': case 'm': case 'N': case 'n': case 'O': case 'o':
printf("6");
break;
case 'P': case 'p': case 'R': case 'r': case 'S': case 's':
printf("7");
break;
case 'T': case 't': case 'U': case 'u': case 'V': case 'v':
printf("8");
break;
case 'W': case 'w': case 'X': case 'x': case 'Y': case 'y':
printf("9");
break;
default:
putchar(ch);
}
}
return 0;
}
Related
I was experimenting with what happens when you remove break statements in a specific switch statement in the code below. I know removing break statements causes execution to flow through to the next case.
But I am confused why the following program will count digits as whitespace characters, see below
int main() {
int c , i , nwhite , nother , ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; i++)
ndigit[i] = 0;
while ( (c = getchar()) != EOF){
switch (c)
{
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
ndigit[c-'0']++;
case ' ': case '\n': case '\t':
nwhite++;
default:
nother++;
}
}
printf("digits= ");
for ( i = 0; i < 10; i++)
{
printf(" %d", ndigit[i]);
}
printf(", white space = %d, other = %d\n", nwhite, nother);
return 0;
}
Input :
farai 1234is 34
Output:
digits= 0 1 1 2 2 0 0 0 0 0, white space = 9, other = 16
Why are the digits '1234' and '34' counting as whitespace?
You removed your break; statements after the operation code.
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
ndigit[c-'0']++;
Very good.
case ' ': case '\n': case '\t':
nwhite++;
But without a break it goes on to the next case label and falls through to the next statement. Thus it increments both ndigit[] and nwhite You really want
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
ndigit[c-'0']++;
break;
so it doesn't do that. (Sometimes falling through is what you want. Just not this time.)
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;
}
I already have a program that converts hexadecimal numbers into its binary form, the only problem is that it does not accept hexadecimals with float. Here is the code:
/* HEXADECIMAL TO BINARY */
#include<stdio.h>
#define MAX 1000
int main(){
char hexaDecimal[MAX], *pch;
long int i=0;
clrscr();
printf("Enter any hexadecimal number: ");
scanf("%s",hexaDecimal);
printf("\nEquivalent binary value: ");
while(hexaDecimal[i]){
switch(hexaDecimal[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': printf("1010"); break;
case 'B': printf("1011"); break;
case 'C': printf("1100"); break;
case 'D': printf("1101"); break;
case 'E': printf("1110"); break;
case 'F': printf("1111"); break;
case 'a': printf("1010"); break;
case 'b': printf("1011"); break;
case 'c': printf("1100"); break;
case 'd': printf("1101"); break;
case 'e': printf("1110"); break;
case 'f': printf("1111"); break;
default: printf("\nInvalid hexadecimal digit %c ",hexaDecimal[i]);
return 0;
}
i++;
}
getch();
return 0;
}
I tried splitting the string using strtok but it doesn't work.. All I need is for this program to also work with hexadecimal fractions. Thanks for the responses in advance!
Just add to your switch block:
case '.': printf("."); break;
Just use strtoul defined in stdlib.h you can convert any radix base to decimal from this method including Hexa-decimal.
I'm trying to create a fiscal code calculator algorithm.
Here's the code:
#include<stdio.h>
#include<string.h>
int main()
{
int Day,Month,Year,i;
char Mo;
char Name[1][30];
char Surname[1][30];
char A,B,C,D,E,H,L,M,P,R,S,T;
printf("Insert your birthday day: ");
scanf("%d",&Day);
printf("Insert your birthday month: ");
scanf("%d",&Month);
printf("Insert your birthday year (last two numbers): ");
scanf("%d",&Year);
/*Month calculator*/
switch(Month)
{
case 1:
Mo="A";
break;
case 2:
Mo="B";
break;
case 3:
Mo="C";
break;
case 4:
Mo="D";
break;
case 5:
Mo="E";
break;
case 6:
Mo="H";
break;
case 7:
Mo="L";
break;
case 8:
Mo="M";
break;
case 9:
Mo="P";
break;
case 10:
Mo="R";
break;
case 11:
Mo="S";
break;
case 12:
Mo="T";
break;
}
printf("Your fiscal code is: %d%c%d",Year,Mo,Day);
}
In every case of the switch i receive the same error: Incompatible pointer to integer conversion assigning to 'char' from 'char[2]'.
Where is the error?
Thanks to all!
You are trying to assign chars to char*s. Mo is a char and strings surrounded in double quotes(") are char*s ending with a \0. Use single quotes(') to denote characters.
Change
switch(Month)
{
case 1:
Mo="A";
break;
case 2:
Mo="B";
break;
case 3:
Mo="C";
break;
case 4:
Mo="D";
break;
case 5:
Mo="E";
break;
case 6:
Mo="H";
break;
case 7:
Mo="L";
break;
case 8:
Mo="M";
break;
case 9:
Mo="P";
break;
case 10:
Mo="R";
break;
case 11:
Mo="S";
break;
case 12:
Mo="T";
break;
}
to
switch(Month)
{
case 1:
Mo='A';
break;
case 2:
Mo='B';
break;
case 3:
Mo='C';
break;
case 4:
Mo='D';
break;
case 5:
Mo='E';
break;
case 6:
Mo='H';
break;
case 7:
Mo='L';
break;
case 8:
Mo='M';
break;
case 9:
Mo='P';
break;
case 10:
Mo='R';
break;
case 11:
Mo='S';
break;
case 12:
Mo='T';
//break; Not needed
}
throwing a 'System.IndexOutOfRangeException' at me in a place it hadn't a year or two ago when i first made this. I asked my current proffessor but he just said "it works there is no problem" We are both getting the exception so idk why the teacher isn't. Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/* Author: Austin Bigge
* This program will ask for a ten digit alphanumeric
* number and return the numeric number.
*/
namespace alphanumeric
{
class Program
{
static void Main(string[] args)
{
// Switch for alphanumeric entry
string numberString; //alphanumberic entry
char currentLetter;
string invalue;
double pnumber; //returned single numeric phone number
int i; //i = the current character space
//User prompt
Console.WriteLine("This program will ask for an alphanumberic phone ");
Console.WriteLine("number and change it into a numeric phone number.");
Console.WriteLine("! Alphanumeric characters only !");
Console.WriteLine("! No Spaces, Dashes, or Symbols !");
Console.Write("What is the 10 digit phone number? ");
invalue = Console.ReadLine();
numberString = invalue;
char[] array = numberString.ToCharArray();
for (i = 0; i < array.Length; i++) //for loop
// gets the current letter at position "i"
currentLetter = array[i];
switch (i) //case switch for possible entries
{
case '1':
pnumber = 1; Console.WriteLine(pnumber); break;
case '2':
case 'a':
case 'A':
case 'b':
case 'B':
case 'c':
case 'C':
pnumber = 2; Console.WriteLine(pnumber); break;
case '3':
case 'd':
case 'D':
case 'e':
case 'E':
case 'f':
case 'F':
pnumber = 3; Console.WriteLine(pnumber); break;
case '4':
case 'g':
case 'G':
case 'h':
case 'H':
case 'i':
case 'I':
pnumber = 4; Console.WriteLine(pnumber); break;
case '5':
case 'j':
case 'J':
case 'k':
case 'K':
case 'l':
case 'L':
pnumber = 5; Console.WriteLine(pnumber); break;
case '6':
case 'm':
case 'M':
case 'n':
case 'N':
case 'o':
case 'O':
pnumber = 6; Console.WriteLine(pnumber); break;
case '7':
case 'p':
case 'P':
case 'q':
case 'Q':
case 'r':
case 'R':
case 's':
case 'S':
pnumber = 7; Console.WriteLine(pnumber); break;
case '8':
case 't':
case 'T':
case 'u':
case 'U':
case 'v':
case 'V':
pnumber = 8; Console.WriteLine(pnumber); break;
case '9':
case 'w':
case 'W':
case 'x':
case 'X':
case 'y':
case 'Y':
case 'z':
case 'Z':
pnumber = 9; Console.WriteLine(pnumber); break;
case '0':
pnumber = 0; Console.WriteLine(pnumber); break;
case ' ': Console.WriteLine("Bad Value, do not use spaces"); break;
default: Console.WriteLine("Bad Value, use only numbers or letters."); break;
}
}
}
}
any help is appreciated, he keeps asking me for help and i keep telling him he knows just as much as me.
p.s. the debugger pointed to line 36 for this exception
EDIT:
yup you're right, now i have an unassigned variable at line 37 "currentLetter", i already initialized it i thought, when i tried to initialize it at the error it told me it was already initialized... i must have deleted my working program. i found a couple empty iterations of this file. I really appreciate the help however, and so will my friend im sure.
i think i fixed it, i replaced "currentLetter" with "i"
and now the array isn't working. only returning the default case. stuck again. i edited my progress.
EDIT
Found out a bunch of errors when we put our heads together. Completed program is as follows.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Switch for alphanumeric entry
string numberString; //alphanumberic entry
char currentLetter;
string invalue;
double pnumber; //returned single numeric phone number
int i; //i = the current character space
//User prompt
Console.WriteLine("This program will ask for an alphanumberic phone ");
Console.WriteLine("number and change it into a numeric phone number.");
Console.WriteLine("! Alphanumeric characters only !");
Console.WriteLine("! No Spaces, Dashes, or Symbols !");
Console.Write("What is the 10 digit phone number? ");
invalue = Console.ReadLine();
numberString = invalue;
char[] array = numberString.ToCharArray();
for (i = 0; i < 10; i++)
{
//for loop
//numberString = invalue.ToString().ToCharArray()[i]);
// gets the current letter at position "i"
currentLetter = array[i];
switch (currentLetter) //case switch for possible entries
{
case '0':
pnumber = 0; Console.Write(pnumber); break;
case '1':
pnumber = 1; Console.Write(pnumber); break;
case '2':
case 'a':
case 'A':
case 'b':
case 'B':
case 'c':
case 'C':
pnumber = 2; Console.Write(pnumber); break;
case '3':
case 'd':
case 'D':
case 'e':
case 'E':
case 'f':
case 'F':
pnumber = 3; Console.Write(pnumber); break;
case '4':
case 'g':
case 'G':
case 'h':
case 'H':
case 'i':
case 'I':
pnumber = 4; Console.Write(pnumber); break;
case '5':
case 'j':
case 'J':
case 'k':
case 'K':
case 'l':
case 'L':
pnumber = 5; Console.Write(pnumber); break;
case '6':
case 'm':
case 'M':
case 'n':
case 'N':
case 'o':
case 'O':
pnumber = 6; Console.Write(pnumber); break;
case '7':
case 'p':
case 'P':
case 'q':
case 'Q':
case 'r':
case 'R':
case 's':
case 'S':
pnumber = 7; Console.Write(pnumber); break;
case '8':
case 't':
case 'T':
case 'u':
case 'U':
case 'v':
case 'V':
pnumber = 8; Console.Write(pnumber); break;
case '9':
case 'w':
case 'W':
case 'x':
case 'X':
case 'y':
case 'Y':
case 'z':
case 'Z':
pnumber = 9; Console.Write(pnumber); break;
case ' ':
Console.WriteLine();
Console.WriteLine("Bad Value, do not use spaces");
Console.Write("What is the 10 digit phone number? ");
invalue = Console.ReadLine();
numberString = invalue;
array = numberString.ToCharArray();
i = 0;
break;
default:
Console.WriteLine();
Console.WriteLine("Bad Value, use only numbers or letters.");
Console.Write("What is the 10 digit phone number? ");
invalue = Console.ReadLine();
numberString = invalue;
array = numberString.ToCharArray();
i = 0;
break;
}
}
Console.WriteLine();
Console.WriteLine("Press any key to close this window");
Console.ReadLine();
}
}
}
Thank you for getting us started with the fixes, we both appreciate it, friend!
The semi-colon on your for line is what's causing your problem:
for (i = 0; i < array.Length; i++) ; //for loop
A for loop should be followed by a block, but in this case, since it's followed by a semi-colon, the code that follows isn't part of the loop, but just regular code that's executed once.
So the for loop executes with an empty body, and when it's done, i has a value of the length of the array, which is of course one higher than the max index, which throws that exception.
To fix this, create a block (curly brackets) around the group of code you want included in the loop.
This also brings up a separate issue - for loops generally will declare the indexer in the loop itself - if you would have defined i in the for, then it would have been out of scope for the following lines, and would have been easier to see your mistake, since it would have been a compiler error.
for (int i = 0; i < array.Length; i++)
{
// Your code here
}
// i is no longer in scope