C++ :: Switch Structures - Program To Arrange Values
Mar 5, 2012
Write a program that accepts three values (int) from the keyboard into three variables a, b and c. After loading the variables, your program will arrange the values into a, b, and c, so that a contains the larger, be the next and c the smallest.
You can use any other variables in addition to a, b c, as you see fit.
You will have to use some variation(s) of the if construct.
There are many ways to program this, but your challenge will be to achieve this without doing all possible comparisons. In fact, three comparisons woudl be enough, if you , for example, determine the largest number first, and keep track of its position in some way , so you can compare the other two.
I think I might use a code similar to the one below.
switch (expression) {
case constant1:
group of statements 1;
break;
case constant2:
[Code] ....
View 14 Replies
ADVERTISEMENT
Dec 14, 2014
I'm trying to learn structure type in C. In this trying, the code must take 9 value from user but it takes 6 values then returned. I scrutinized but I can't find my error.
#include <stdio.h>
typedef struct books {
char name;
float price;
int pages;
} book;
[code].....
View 2 Replies
View Related
Sep 26, 2014
typedef struct example_dt_struct {
int a;
float b;
char* c;
};
typedef void(*func)(example_dt_struct *s, int e, int f);
void f(func *n){}
how can i use example_dt_structure with my function pointer into f()
View 5 Replies
View Related
Mar 21, 2015
My code is supposed to read a five to four digit code of a resistor from a file and determine the resistor's nominal, lower and upper tolerance values. I have inputted my file's first resistor code as a string so I could run tests on it but the problem that occurs is that a get the wrong values from my switch-case statements. I have put printfs after the function call to see what the values were and they turned out wrong. If I could get my return values to be right then I could be on my way coding.
#include<stdlib.h>
#include<stdio.h>
double bandNum(char x); // function prototype that will read the resistor value for the first, second and possibly third band
double bandMult(char x); // function prototype that multiplies the resistor by some value of 10
double bandTol(char x); // function prototype that the tolerance of the resistor is multiplied by
[Code] ....
View 5 Replies
View Related
Feb 26, 2015
I just wanted to ask the reason that why is the below code not checking the case -1 while working for the other case values.
#include<stdio.h>
#include<conio.h>
int main() {
int i=-1;
switch(i-2) {
[Code] ....
So why in the below code the case -1 doesnt run,when the value evaluated by the switch construct is actually a negative integer constant.
View 1 Replies
View Related
Sep 22, 2014
for our school project, we have to make a program which involves three students and the prices each of them paid for the meals. The program has to calculate how much each of the students has to pay/receive to/from the others. Most of it, we got from our teacher. The error we are getting is that we have to 'take care of the input/output order'. So if you type in the names/numbers of the students in a different way, so for example if you type first number 1, then 0, then 2 and 0 and 1 have to pay money to number 2, number 0 has to be in front of 1. Our output is that number 1 is in front of number 0.
insert
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
[Code].....
View 1 Replies
View Related
Oct 25, 2013
I am having problem with comparing first letter of every wordso that i can arrange them in array.
Code:
/*22/10/13 15:30
Arrange set of names in an array in alphabetical order
*/
#include<stdio.h>
main( ) {
int x,a,i=0,j;
char *temp, *str[]={
[Code] ....
I am getting unexpected output here :
Code:
himanshu
amit
nitin
saurabh
View 3 Replies
View Related
Feb 25, 2013
I'm trying to arrange names alphabetically from a text file. Inside my textfile, it's like : "John", "Edward", "Peaches", "Anna"... etc.(It has thousand more names, all with quotations, separated by comma and I think it's not typed line by line... but using only one line). How can I read each name separately?
this code, it works if the text file contains names separated by lines and without commas and quotations. I do not know with the case I've stated above.
string numbers[1000];
string line;
string number;
string y;
string x;
int z = 1;
ifstream myfile;
myfile.open("names.txt");
[Code]...
This only shows the data inside text file. I'm new in c++.
View 2 Replies
View Related
Mar 1, 2014
I have an assignment to write a program using structures, getting input from a .txt file and output in text file as well. I have written a program that only reads and prints the data. I am trying to test if it works fine or not. I always comment an unnecessary code but still it wont compile. Now whats wrong with this program?
/* Bank Accounts Program */
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>//needed to use system() function
using namespace std;
const int MAX_NUM = 50;
void menu();
[Code]...
View 1 Replies
View Related
Feb 17, 2015
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
[Code]....
when running the program it closses after the switch.
View 2 Replies
View Related
May 10, 2012
How can we use switch statement 2 times in a program?
View 2 Replies
View Related
Mar 22, 2013
How to put my all assignment questions that are 6 in number in one program by using switch statement ?
so if a user wants to see any question by choice!
View 1 Replies
View Related
Dec 30, 2014
I am trying to make a program which manages the data of 5 different patients (its an uni assignment), and i want to use a switch case as a menu to switch between the patients. All other functions (as for example putting the infromation on a file) work, but i cant figure out to bring the switch to work. First it asks for the number of the patient which should be worked with, this works perfectly, but afterwards changiung between the persons doesnt work as thought. It should ask everytime after it switches to one patient (i removed some functions to make it easier to read) and then asks to which it should jump next. If i put the number of one case (lets say 3) it just stops the program.
********Example:
user@pc ~/wherever $ ./program
current variables:
jo = 2
a = 0
1 //the entered number
The variable a = 1
patient 1
Enter the number of the next patient2
// and then it closes
**************
The same thing happens if i compile an example code from a book, it writes the first case and then stops.
I looked already through the forum but didnt find a person with a similar problem, maybe i didnt dig deep enough.
I am running Linux Mint 17 and use gcc as compiler.
Code:
include <stdio.h>
#include <stdlib.h>
int a=0;
int jo=2;
int main(void){
printf("current variables:
jo = %d
a = %d
[Code]...
View 13 Replies
View Related
Jun 18, 2013
I am doing an exercise which has to do with International country codes.The user must give a code and the programm will display the corresponding country.
Code:
#include <stdio.h>
#define COUNTRY_COUNT
((int) (sizeof(country_codes) / sizeof(country_codes[0])))
[code]....
View 14 Replies
View Related
Dec 6, 2014
I'm writing a Blackjack program where the user can choose to Hit(H) or Stand (S). I would like them to be able to use upper or lower case letter for this. My code doesn't seem to work right for my Stands.
Code:
char choice;
do {
choice = 0;
while (choice < 1) {
printf("
[Code] ....
View 3 Replies
View Related
Feb 13, 2015
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.".
View 7 Replies
View Related
May 4, 2014
I'm trying to write a program that has a menu with a switch statement. I'm getting "case label'1' not within a switch statement" and subsequent errors for the other two choices and the default.
int main(){
int choice;
Matrix A,B,C;
cout <<"What would you like to do? "<< endl;
[Code]....
View 2 Replies
View Related
Aug 12, 2013
i am making a program that reads a file from .txt and print them out using linked list. However, i need to sort them from the highest price to lowest price.
Code:
/* my structs */
typedef struct{
Node *head;
Node *tail;
Node *iterator;
int size;
} List;
[Code]...
i know its long but im afraid that i might miss out some things.
View 12 Replies
View Related
Apr 21, 2014
I am trying to build a c++ that reads user input and arrange letters in ascending order.
for example, if the user input: Hello my name is Moe! the output will be: !aeeehillmmmnoos (ascending order)
my problem is that when i input hello my name is moe the output will be ehllo (not completing other letters) also when i change class size to 50, it outputs unknown weird letters.
This is my code:
#define CLASS_SIZE 10
#include <stdio.h>
#include <iostream>
void bubbleSortAWriteToB(const char a[], char b[]);
using namespace std;
int main(void){
int i;
[Code]...
View 3 Replies
View Related
Oct 21, 2013
How to write a program that will prompt and read 5 numbers. Then the program should print numbers that are larger than 80.
View 1 Replies
View Related
Apr 18, 2013
Trying to write a program that calculates a students grade based on how many assignments have been graded. I am using a switch case since there is a total of 5 assignments, however when I enter case 1, and enter in how many I got out of 100, it just closes the program it doesn't go to the next part.
#include <iostream>
using namespace std;
char calculategrade(int total);
int main() {
// local variable declaration:
char input;
[Code]....
View 6 Replies
View Related
Oct 1, 2014
I am returning area from areaCircle(), but do not know how to print it in main. I know this program has lots of errors, two cases that I have listed.
Not sure how to properly list multiple function calls in a switch statement, and how to print one's return from main.
int main() {
char choice;
double area;
showMenu(choice);
switch (choice) // If input is C, use getRadius, areaCircle and count, then print the are
{
case 'C': void getRadius(),double areaCircle(), void count(bool display = false);
[Code]...
View 5 Replies
View Related
Dec 4, 2013
This is my code: [tag]
Code:
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
[Code] .....
View 4 Replies
View Related
Mar 25, 2013
I am writing a snakes and ladder program and I'm almost finished, but I am struggling with the dice to work in the way I want it to work. I want the dice to work like this :
Before each player throw the dice they must start at 0.
Each player must throw a 6 on the dice to move on the board.
If a player threw a 6 on the dice, that player can throw again.
But I ended up with two seperate dice, one for each player (game is only for two players).And when I run the program, both players don't start at 0. And when I throw the dice, both players move at the same time but with different values.
If one of my players threw a 6, they just keep on throwing until someone wins the game. I tried to use a switch and if statements but I couldn't get it right. And so I did this :
Code:
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#define SpaceBar 32// 32 is an ASCII value for a spacebar
[Code] ....
View 5 Replies
View Related
Dec 11, 2013
I am writing a employee payroll program using structures. My program is running but its only showing some of the data.
HERES MY CODE
Code:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstring>
using namespace std;
const int SZ = 20; // size of arrays to hold scores
struct payrollStruct {
[Code] ....
And it doesn't show anything from txt file
Code:
40.0 10.00 A1234 Jane Adams
50.0 10.00 L8765 Mary Lincoln
25.5 10.85 W7654 Martha Washington
52.0 15.75 A9876 John Adams
45.0 25.00 W1235 George Washington
[Code] .....
View 14 Replies
View Related
Dec 4, 2013
I have a program that stores health information the user inputs, one person at a time. The program works perfectly with the exception of storing the data...I need to open a file and read what health data it has in it already, if any, but store the new changes, and appended data to the array of structures, to the data in memory. All of the information is only saved back in the file once the program terminates. I'm not sure how to go about doing this, so I am also not sure what to put in the function for "Save and Exit" that the user can choose in order to exit the program.
Code:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
struct strdatabase
{
char first[20];
char last[20];
char gender[2];
[Code]...
I tried playing around with this bit of code, but I'm not sure if I'm even on the right track.
Code:
FILE *fp;
fp = fopen ("students.txt","rb"); fseek(fp, 0, SEEK_END);
long pos = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *bytes = malloc(pos);
fread(bytes, pos, 1, fp);
fclose(fp);
View 9 Replies
View Related