I'm working on that famous program that gives the date of tomorrow so I chose to use a switch to determine the months with 30 days in one case and the months with 31 days in another.
When I write case 2: it works, but when I try to write case : 4,6,9,11 it shows me an error. How can I put multiple values in one case?
switch (m) {
case 2:
do {
printf("faire entrer le jour en chiffres ");
scanf("%d", &j);
} while (j > 29 || j < 1);
if (j == 29) {
j = 1;
m = m++;
a = a;
} else {
j = j++;
m = m;
a = a;
};
break;
case (1||3||5||7||8||10||12) :
do {
printf("faire entrer le jour en chiffres \n");
scanf("%d", &j);
} while (j < 1 || j > 31);
if (j == 31) {
j = 1;
m = m++;
a = a;
} else {
j++;
m = m;
a = a;
};
break;
case 4 6 9 11 :
do {
printf("faire entrer le jour par chifre \n");
scanf("%d", &j);
} while (j < 1 && j > 30);
if (j == 30) {
j = 1;
m = m;
a = a;
} else {
j = j++;
m = m;
a = a;
};
break;
}
You need to use separate case labels like
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
do {
//...
Or the above line can be rewritten like
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
do {
//...
Pay attention to that there is no sense to place a null statement after the closing brace as you are doing as for example
}else{
j=j++;
m=m;
a=a;
} ; break
^^^^
It is better to write
}else{
j=j++;
m=m;
a=a;
}
break
#Vlad from Moscow well answers how to use a switch case with multiple values.
Yet given the repetitive nature of OP's code, consider a different solution than switch.
if (m < 1 || m > 12) {
Handle_Error();
} else {
static int eom[] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
do {
printf("faire entrer le jour par chifre \n");
scanf("%d",&j);
} while(j<1 || j>eom[m]);
if(j == eom[m]) {
j = 1;
m++; // add code for December to adjust `a`
} else {
j++;
}
}
Of course we still need some more changes for February and leap years.
Related
I have a function for translating the fractional part of the entered number into another number system (not the most beautiful code):
void FracToAny(double D, int q, int t)
{
double b[10] = {0};
for (int i = 1; i < t; i++)
{
double DP = D * q;
D = modf(DP, &b[i]);
if (D == 0)
{
break;
}
D = DP - b[i];
}
for (int i = 0; i < t; i++)
{
if (q == 16)
{
switch ((int)b[i])
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
default:
printf("%d", (int)b[i]);
break;
}
}
}
}
As a result, I get an array of double, which is a character-by-character representation of the resulting fractional part. However, I would like to return this value from the function in the form of "0.result". How can I do this?
It would be good not to use union or dynamic memory allocation (I've seen them trying to find a solution to the problem). I'd like to make it as simple as possible.
I think this is what you are trying to do (see my comment above):
Mac_3.2.57$cat etFract.c
#include <stdio.h>
#include <math.h>
int main(void)
{
int i;
double D;
int t = 3;
double b[10] = {0};
D = (3 * 16*16*16 + 4 * 16*16 + 5 *16)/(16*16*16.0);
printf("translating %f...\n", D);
for(i = 0; i < t; i++){
b[i] = modf(D, &D);
if(D == 0){
break;
}
D = b[i] * 16;
}
printf("0.");
for(int i = 0; i < t; i++){
switch((int)b[i]*16){
case 10:
printf("A\n");
break;
case 11:
printf("B\n");
break;
case 12:
printf("C\n");
break;
case 13:
printf("D\n");
break;
case 14:
printf("E\n");
break;
case 15:
printf("F\n");
break;
default:
printf("%d", (int)(b[i]*16));
break;
}
}
printf("\n");
return(0);
}
Mac_3.2.57$cc etFract.c
Mac_3.2.57$./a.out
translating 3.269531...
0.450
Mac_3.2.57$
I am trying to make a built-in digital clock in a C program.
This is the code I used:
int wmain(void) {
time_t t;
t = time(NULL);
struct tm tm = *localtime(&t);
int m;
printf(" %d ", tm.tm_mday);
m = tm.tm_mon+1;
switch(m)
{
case 1:
printf("Jan, ");
break;
case 2:
printf("Feb, ");
break;
case 3:
printf("Mar, ");
break;
case 4:
printf("Apr, ");
break;
case 5:
printf("May, ");
break;
case 6:
printf("June, ");
break;
case 7:
printf("July, ");
break;
case 8:
printf("Aug, ");
break;
case 9:
printf("Sep, ");
break;
case 10:
printf("Oct, ");
break;
case 11:
printf("Nov, ");
break;
case 12:
printf("Dec, ");
break;
}
printf("%d", tm.tm_year+1900);
printf("\n ");
if(tm.tm_hour>=12)
{
if(tm.tm_hour==12)
printf("12");
else
printf("%d", tm.tm_hour-12);
printf(":%d PM\n", tm.tm_min);
}
else
printf("%d:%d AM", tm.tm_hour, tm.tm_min);
//getch();
return 0;
}
The problem with this code is that you have to refresh every minute in order for the clock to update.
I have another code here:
void clock()
{
int h = 2, m = 41, s = 12;
int d = 1000;
while(1){
s++;
if (s > 59){
m++;
s = 0;
}
if (m > 59){
h++;
m = 0;
}
if(h>12){
h = 1;
}
printf("%02d : %02d : %02d", h, m, s);
Sleep(d);
system("cls");
//menu();
}
printf("\n\n");
}
but this code goes into an infinite loop and doesn't display anything else but the clock.
So, how can I update the seconds in the clock without going into infinite loop?
#include <stdio.h>
#include <conio.h>
#include <Windows.h>
DWORD WINAPI digclock()
{
int h = 2, m = 41, s = 12;
int d = 1000;
while (1) {
s++;
if (s > 59) {
m++;
s = 0;
}
if (m > 59) {
h++;
m = 0;
}
if (h > 12) {
h = 1;
}
printf("%02d : %02d : %02d", h, m, s);
Sleep(d);
}
printf("\n\n");
return;
}
void main()
{
HANDLE clockThread = CreateThread(NULL, 0, digclock, NULL, 0, NULL);
if (clockThread == INVALID_HANDLE_VALUE) {
printf("Error creating thread.\n");
}
while(1)
{
printf("main continue without block \n");
Sleep(500);
}
return;
}
Try above code, basically your clock should be running in separate thread. create thread API may differ in your environment.
i wrote a simple barbut game in long way.
I want to learn something, if get in 42. line; when the game over, it doesnt go to 84. line.
If the game doesnt get in 42. line, when game is overgoes to 84. line.
int main() {
setlocale(LC_ALL, "Turkish");
int zar1 = 0;
int zar2 = 0;
int i = 1;
int a = 1;
int toplamzar = 0;
int oyuncununzari = 0;
while (i == 1) {
srand(time(NULL));
zar1 = 1 + (rand() % 6);
zar2 = 1 + (rand() % 6);
toplamzar = zar1 + zar2;
printf("\n****ZAR 1: %d \n****ZAR 2: %d\n", zar1, zar2);
switch (toplamzar) {
case 7: case 11:
printf("%d attınız ve kazandınız.", toplamzar);
break;
case 2: case 3: case 12:
printf("%d attınız ve kaybettiniz.", toplamzar);
break;
case 4: case 5: case 6: case 8: case 9: case 10: {
printf("%d sayısı sizin sayınız.", toplamzar);
oyuncununzari = toplamzar;
toplamzar = -2;
printf("\nZar atın:");
scanf("%d", &a);
while (toplamzar != oyuncununzari || toplamzar != 7) {
while (a == 1) {
zar1 = 1 + rand() % 6;
zar2 = 1 + rand() % 6;
toplamzar = zar1 + zar2;
printf("\n****ZAR 1: %d \n****ZAR 2: %d\n", zar1, zar2);
if (toplamzar == oyuncununzari) {
printf("%d attınız, kazandınız.", toplamzar);
a = 0;
}
else
if (toplamzar == 7) {
printf("%d attınız, kaybettiniz.", toplamzar);
a = 0;
}
else
if (toplamzar != 7 && toplamzar != oyuncununzari) {
scanf("%d", &a);
}
}
}
break;
}
}
printf("\nTekrar oynamak ister misiniz?");
scanf("%d", &i);
}
return 0;
}
You need to change line :
while (toplamzar != oyuncununzari || toplamzar != 7)
to :
while ( (toplamzar != oyuncununzari) && (toplamzar != 7) )
so that the loop executes when both conditions are true. Right now it executes even if just one of them is true.
I've tried two-dimensional arrays, varying prototype functions, and I can't seem to implement a score system for this game. Any ideas on what I can do? I want to take the output from this code, which are 6 varying numbers from 1-6, and assign them a value that I can add up to make a score. Ex. If I roll a 1, it would be worth 100 points. What's the quickest and most efficient way to assign a point value to the rolled value?
#include <stdio.h>
#include <time.h>
int main() {
int i, r, diceRoll;
char player1[20];
int temp, swapped;
int roll1[6];
srand(time(NULL));
printf("Enter name for Player 1:\n");
scanf(" %s", &player1);
for(i = 0; i < 6; i ++) {
r = ( rand() % 6 ) + 1;
diceRoll= r;
roll1[i] = diceRoll;
}
while(1) {
swapped = 0;
for( i = 0; i < 6-1; i++ ) {
if( roll1[i] > roll1[i + 1] ) {
temp = roll1[i];
roll1[i ] = roll1[i+1];
roll1[i+1] = temp;
swapped = 1;
}
}//for
if( swapped == 0) {
break;
}
}//while
printf("\n\n %s's Dice Roll:\n\n", player1); // prints out the dice rolls of player 1
return 0;
}//main
Why not do a straight aggregation of the values mapped to the corresponding point value? (Unless it is straight 100 * the roll value, in which case it is even easier.)
int score = 0;
for( i = 0; i < 6; ++i )
{
switch( roll1[i] )
{
case 1: score += 100; break;
case 2: score += 230; break;
case 3: score += 540; break;
case 4: score += 2320; break;
case 5: score += 13130; break;
case 6: score += 454260; break; /* Of course, change to the score you want for each value. */
}
}
printf("\n\n %s's Dice Roll:%d\n\n", player1, score);
"What I originally had in mind was to make the game like Farkle, where 1 is 100pts, 5 is 50pts, and everything else is 0 until you get 3 of a kind ( Three 3s = 300pts, Three 1s is 1000, etc). Any input would be greatly appreciated" There are many ways. You can do this easily and use Chux map approach for base and three of a kinds.
int score = 0;
int face_score = 0;
int base_points[6] = { 100, 0, 0, 0, 50, 0 };
int three_of_a_kind_points[6] = { 300, 200, 300, 400, 500, 600 };
int four_of_a_kind_points[6] = {
int repeat_counter[6] = { 0, 0, 0, 0, 0, 0 };
int kind_mask = 0;
int pair_count = 0;
int three_of_a_kind_count = 0;
int four_of_a_kind_count = 0;
for( i = 0; i < 6; ++i )
{
kind_mask |= 1 << ( roll1[i] - 1 );
switch( ++repeat_counter[roll1[i] - 1] )
{
case 1: break;
case 2: ++pair_count; break;
case 3: score = three_of_a_kind_points[rolli[i] - 1]; ++three_of_a_kind_count; break;
case 4: score = 1000; ++four_of_a_kind_count; break;
case 5: score = 2000; break;
case 6: score = 3000; break;
}
}
if( pair_count == 3 ) /* Three pairs */
score = 1500;
else if( three_of_a_kind_count == 2 ) /* Two three of a kinds */
score = 2500;
else if( four_of_a_kind && ( pair_count == 2 ) ) /* A four of a kind and one pair (2 because four of a kind counted as a pair in the aggregation phase) */
score = 1500;
else if( kind_mask = 0x2F ) /* One of each type */
score = 1500; /* Straight */
else if( !score ) /* score only 1's and 5's */
score = repeat_counter[0] * 100 + repeat_counter[4] * 50;
printf("\n\n %s's Dice Roll:%d\n\n", player1, score);
I have not compiled or run this code, so it may not be 100% correct.
This question already has answers here:
Undefined reference to `sin` [duplicate]
(4 answers)
Closed 9 years ago.
../NoteConverter.c: In function ‘main’:
../NoteConverter.c:154:9: warning: variable ‘position’ set but not used [-Wunused-but-set-variable]
Finished building: ../NoteConverter.c
Building target: NoteConverter
Invoking: GCC C Linker
gcc -o "NoteConverter" ./NoteConverter.o
../NoteConverter.c:21: error: undefined reference to 'sin'
collect2: error: ld returned 1 exit status
make: *** [NoteConverter] Error 1
The following is code
/**
* Frequency octave finder and play note
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#define DURATION 5
#define SAMPLERATE 48000
#define AMPLITUDE 1
//A*sin(2*pi*n*f/R)
double yCord(int n, double freq)
{
double y= sin(2*M_PI*freq*n/48000);
return y;
}
int xCord(int n)
{
double x = n/SAMPLERATE;
return x;
}
// finds the freq from switch
int noteBaseFeq(char *note) {
int baseNote = 0;
int baseFreq = 0;
const char *notes[] = { "C ", "C#", "Db", "D ", "D#", "Eb", "E ",
"F ", "F#", "Gb", "G ", "G#", "Ab", "A ", "A#", "Bb", "B " };
int i;
for( i = 0; i < 17; i++) {
if (strcmp(note,notes[i]) == 0) {
baseNote = i;
break;
}
}
switch (baseNote+1) {
case 1: // c
baseFreq = 26163;
break;
case 2: // c#
case 3: // Db
baseFreq = 27718;
break;
case 4: // D
baseFreq = 29366;
break;
case 5: //D#
case 6: //Eb
baseFreq = 31113;
break;
case 7: //E
baseFreq = 32963;
break;
case 8: //F
baseFreq = 34923;
break;
case 9: //F#
case 10: //Gb
baseFreq = 36999;
break;
case 11: //G
baseFreq = 39200;
break;
case 12: //G#
case 13: //Ab
baseFreq = 41530;
break;
case 14: //A
baseFreq = 44000;
break;
case 15: //A#
case 16: //Bb
baseFreq = 46616;
break;
case 17: //B
baseFreq = 49388;
break;
}
return baseFreq;
}
// finds the octive based on A 4
int octaveModifier(int oct)
{
if (oct == 4) {
return oct;
} else if (oct < 4) {
oct = 4 - oct;
} else if (oct > 4) {
oct = oct - 4;
}
return oct;
}
// allows for concatenation
void append(char* s, char c)
{
int len=strlen(s);
s[len]=c;
s[len+1]='\0';
}
int main()
{
char note;
char modifier;
char inputNote[256] = "";
long long intFreq = 0;
double freq = 0.0;
int oct = 0;
int modOct = 0;
printf("Please enter an note sharp\\flat(\"b\" for flat) and octave: \n");
scanf("%c%c%d", ¬e, &modifier, &modOct);// gets input from user
append(inputNote, note);
append(inputNote, modifier);
oct = octaveModifier(modOct);
intFreq = noteBaseFeq(inputNote);
int i;
if(modOct > 4) {
for(i = 0; i < oct; i++) {
intFreq = intFreq * 2;
freq = intFreq / 100.0;
}
} else if(modOct < 4) {
for( i = 0; i < oct; i++) {
intFreq = intFreq / 2;
freq = intFreq / 100.0;
}
} else {
freq = intFreq / 100.0;
}
double time = 0;
double position = 0;
for (i = 0; time < DURATION; i++) {
time = xCord(i);
position = yCord(i, freq);
}
return 0;
}
You probably need to link with -lm to get the maths library linked. Some platforms require that; others do not.
Fixing the set-but-unused variable is easy; remove its declaration and the line where it is set (or use the result — print it, perhaps?).