If I have an integer variable like int a=9 then in the switch case If i write :
switch(a) { case 4+a: printf("hii"); }
Then why is this statement a compile-time error that variables cannot be used inside a case statement why does the compiler not subtitutes the values in place of the variables.
#include <iostream> // For stream I/O using namespace std; int function(int a) { return a; } int main() { function(int b); }
Why is creating a variable inside the function argument list not allowed. Any reason other then for the language syntax or just for the language syntax?
I just dont know how to get my upper case and lower case equal...heres the program: Write a program to calculate utility cost for ACME UTILITY COMPANY. The company has 3 types of customers (R) residential, (C) Commercial and (G) government. Customers are billed based on the number of kilowatts used and they type of customer they are (R,C,or G).
Residential cusstomers are charged 0.25 cents per kilowatt hour for the first 500kw used, and 0.35 cent per kwh for all kw used over 500.Commercial customers are charged 0.22 cents per kilowatt hour for the first 999kw used, 0.29 cents per kwh for all kw used over 999 kw up to 1999 and 0.45 cents per kwh for all kilowatts used greater than 1999. Commercial customers that use over 2000 kw are charged a special surcharge of 100.0 in addition to the regular charges.
Government customers are charged 0.34 cents for the first 1500 kwh used(<=1500). 0.30 cent for the next 1000 kwh(1501-2500) and 0.25 cents for all kwh used over (>=2501)
in addition residential customer are charged .5% tax on the cost utilities while Commercial customers are 5% tax on the cost of utilities not including the special surcharge Government customers are not charged a tax
Code:
#include <stdio.h> #include <math.h> int main(void) { int R; int ct; int kwh; int taxes; int surcharge; int charge }
[code].....
the program should quit with Q and calculate the amount owed, utility charge, and surcharge
I have this case statement that is not performing as expected. For some reason ASE_OK and ASE_NotPresent both print out. Why is this? ASE_OK is a return value of 0 while ASE_NotPresent is -1.
Code: // Try to create the buffers and store it switch (theAsioDriver->createBuffers(info, numChannels, bufferSize, callbacksInfo)) { case ASE_OK: std::cout << "ASE_OK"; input->bufferSize = output->bufferSize = bufferSize; // Buffer size is stored in both input and output case ASE_NotPresent: std::cout << "ASE_NotPresent"; errorMessage = "Could not find input or output devices."; return NO_DEVICES_FOUND;
I am building a tamagotchi like application/game. As for the leveling up and changing of character I am using a char value so that the icon can change as the character progresses.
Due to the fact that the value for level is changing I need it to also change for what is being used for the case statements
I know I cant use variables in case statements because I have tried every way I can think of.
The code is
switch(Area[y][x]) { case '*': Area[y][x] = Level; break; case 1: { CODE } }
Where I have the case 1 I need it to change as level increases (as level is starting at 1)
Ran into something today that does not make sense:
This compiles: Code: int x = 5;
switch(x) { case 0: { int value = 5; } break;
[Code] ....
Ok so it doesn't like int value = 6 b/c of int value = 5 for case 0. However since the value in case 0 is declared within the brackets one would think it has case scope.
So I tried this:
Code: int x = 5; switch(x) { case 0: { int value = 5; } break;
[Code] ....
Now it doesn't like it b/c value has not been declared in case 1:. These two conditions cannot possibly be both true at the same time. You cannot disallow the declaration of value in case 1 b/c it interferes with value in case 0 and at the same time disallow me to use value from case 0 b/c it is not in scope. If it was not in scope then theoretically I should be able to declare value in case 1.
Both MSVS 2012 and 2013 exhibit the same behavior. I checked the standard and it is unclear on the matter.
Im going through a C tutorial and im going to create a Database as part of the course. Im just in the very beginning of the project so its a very simple program so far.
Code:
#include<stdio.h> main(){ /*Variabler for att lagra information om skiva*/ char title[200]; char artist[100]; int tracks; int price;
[Code] ....
The first switch case statement takes me to the menu of choice, 1 or 2. However, chosing menu option one and trying to input a title or artist name, the program crashes.
Next, going into menu option 2 and then trying to print out, for example, title nothing happend. The process just ends.
Again, im sure this simple prototype is full of errors ....
I was just wondering if a switch statement can work without writing each case, for instance:
{ int age; age=18; while (age!=0) { cout<<"Enter age :"<<endl; cin>>age; switch (age) {
[Code] ....
It works only for values specifically entered as a case, for instance if you enter 5 or 15 , it says bicycle, but if you enter anything inbetween it goes to default. Same with 18 and 100. It can't possibly be that you have to enter
case 19: case 20: case 21: case 22:.......up to 100 to include all possible cases.
bool simpleQuestion::checkAnswer(string guess) const { for (int i=0;i<qAnswer.length();i++) if (qAnswer==guess) return true; else return false; }
This is my code and what im trying to accomplish here is making the comparison of the two strings (qAnswer & guess) case insensitive. I know i need to loop through each character of each string and for each character i can use toupper and every char in each string will become uppercase and therefore case insensitive. However, im not sure how to go about this; if any technique used to loop through each character of the string and how to use to upper.
I can get the first letter to change if there is only one word. But if there are two words it wont change and display the second word's first letter and I'm not sure why.
I'm trying to convert the enum type {PG, R, G, PG-13, etc.} to strings so i can use it in cout statement but no matter what i put inside switch(), the compiler keeps saying Error: expression must have integral or enum type. What am I doing wrong exactly?
Movie covert_rating(Movie r) { switch (r) { case PG: return "PG"; break; case R: return "R";
So, I'm going to write a recursive function for a asterisk pattern but I'm stuck figuring out how to create a stopping case for it, better yet, I'm unable to describe the pattern completely.
* Every odd row has 1 * with 1 incremented white space
* Every "pair" of asterisks equals 8 total (EX. 8 one pair *'s, 4 two pair *'s, 2 four pair *'s)
Unfortunately, that's all I got. how I can represent this as I function. Once I figure out what my stopping case should be, I think I can do the coding on my own.
This program was running at first but when I started to change the couts and cins to fouts and fins (in order for them to be save in a file directory), it shows a lot of errors such as:
initialization of 'fin' is skipped by 'case' label 'std::ifstream fin': redefinition
Here is the code of the program:
#include <iostream> #include <iomanip> #include <fstream> using namespace std; struct file{ int fnum; char fname[50], lname[50];
I tried to write a menu program as a switch-case structure with a separate function for a switch-case structure itself. The outcome for it currently is an undesired one
Code:
#include <iostream> using namespace std; int menu(int answer); int main()
[Code].....
The output I get is one where it's just an infinite loop of "Bad choice! Please try again later.".
So I have started this program for class but am stuck on what to do next. Basically you need to create a function to compare 2 strings (case-insensitive). The strings shouldn't be altered and the return value should be similar to strcmp.
This is my main file
#include <iostream> //#include <cstring> #include <iomanip> #include "rpstrings.h" using namespace std; int main () { char Str1[20], Str2[20]; short result;
[Code] .....
I don't know if I labeled the files correct. what I need to do is to add 2 defaulted arguments which will allow the user to request that you skip spaces and/or skip punctuation when doing the comparison.
Also, how I can sort the numbers when they aren't justified with leading 0's and if they aren't in the lead string (kinda of like the natural sort).
was making a somewhat of a Binary to Hex convertor but only 10/15 cases work and the non working are in the middle; 0010, 0011, 0100, 0101, 0110, 0111;;
// Test Code.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <cmath> #include <conio.h> using namespace std; int main(void) {int A; cout << "Enter the binary starting with the MSB
#include <stdio.h> int main () { int num; char choice; printf(" Welcome to a Menu-Demo Program Please select one of the following actions R - Programmers Rock! F - Programming is Fun! G - Geeks Rule! X - Exit program Selection ==>__ "); do{ choice = getchar(); switch(choice)
[code]......
I am currently writing a simple program that utilizes switch statements. When I run my program, it always displays the message for the default case.