I am a beginner in programming and am just starting to write programs that are complex enough where prototypes would be helpful. The problem is when I write my program, compile and run it the functions that are prototyped are blank so none of my cout or cin
prompts in the later defined functions appear.
In this code there is only one portion of the switch statement done in which the CubeVol function is prototyped and then later defined.
This code does compile and when it is run it shows the menu. When I type "2" the program ends without couting "length of cubes side" or asking for an input.
if it matters, I'm using Cygwin with gnu g++ compiler and notepad++ to write the code, which is saved as a .C file. I've also tried formatting it as a .cpp
Keep in mind that the mostly blank switch statement is for later when I actually finish the program.
how do i prototype the CubeVol Function Correctly?
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
double CubeVol();
int main()
{ int choice=0;
cout<< " 1. Sphere \n 2. Cube \n 3. Cone \n 4. Cylinder \n 5. Prism \n 6. Exit \n";
cin>> choice;
switch (choice){
case 1:
;
break;
case 2:
CubeVol;
break;
case 3:
;
break;
case 4:
;
break;
case 5:
;
break ;
case 6:
;
;
break;
};
}
double CubeVol ()
{
double side=0.0; double cubev=0.0;
cout<< "length of cubes side";
cin>> side;
cubev= pow(side,3);
return cubev;
}
You are not calling the function CubeVol() in case 2:
case 2:
std::cout << CubeVol() << "\n"; //call the function and print return value
break;
Also the return value is not being utilized.
Related
So i make a turn based dos game, And i have a switch() function which gives me a bug.. :
int hp;
int mana;
do
{
cout<<"Enter your arg here";
cin>>choice;
cout<<"Hahaha that won't stop me";
switch(choice)
{
case 1:
mana--;
mana--;
hp--;
hp--;
cout<<"Woosh";
}
}
while(1)
{
cout<<endl;
}
Ok so let me explain the bug :
When the player inputs choice variable it will just skip the switch() function an just go continue with cout<<"Hahaha that won't stop me";. How do i fix that?
PS : Sorry for my bad english, and if there is a misswriting on this post. I'm so sorry about that.
First of all, switch is not a function, it's a statement. That means, it doesn't behave like functions. What you've written here doesn't match with what you want.
What the program does is:
printing the first line
reading the input
printing the second line
switching on the input
What you want is:
printing the first line
reading the input
switching on the input
printing the second line on default case
So let's reorder your code.
cout << "Enter your arg here";
cin >> choice;
switch (choice)
{
case 1:
mana -= 2;
hp -= 2;
cout << "Woosh";
break;
default:
cout << "Hahaha that won't stop me";
break;
}
And don't forget to write break; on every case, including default one.
After the call for Back to Main Menu, it returns to the mainMenu but when option or command is typed, the option is not accepted or the loop not working. Wonder where is the mistake? Is it extra call should be added or?
#include <iostream>
using namespace std;
char mainMenu(void);
int factorial(int n);
unsigned long long combination(long nK, long nR);
int main(){
char option;
int shape,function,i,j,k,t,n;
long nK, nR;
unsigned long long COM;
while((option=mainMenu())!='0')
{
switch(option)
{
case '1'://Program 1:
cout<< "*Drawing a shape\n"
<< "(1-Rectangle, 2-Triangle, 3-Inverted Triangle, 4-Letter 'H', 0-Back to Main Menu)\n";
do
{
cout<< "Choose shape >> ";
cin>> shape;
cout<< endl;
switch(shape)
{
case 1: break;
case 2: break;
case 3: break;
case 4: break;
case 0:
//Back to Main Menu
cout<< "Back to main menu\n"
<< endl;
return mainMenu(); //After here, it does back to Main Menu but command or option is not working
}
}while(shape!=0);
case '2': //Program 2
cout<< "*Choose function of calculator\n"
<< "(1-Factorial, 2-Combination, 0-Back to main menu)\n";
do
{
cout<< "Choose function >> ";
cin>> function;
cout<< endl;
switch(function)
{
case 1: break;
case 2: break;
case 0:
cout<< "Back to main menu\n"
<< endl;
return mainMenu();
}
}while(function!=0);
case '0':
cout<< "Program is terminating\n"
<< endl;
return 0;
default:
cout<< "Wrong input. Please choose one of the above options.\n"
<< endl;
return mainMenu();
}
}
}
char mainMenu(void){
char option;
cout<< "##############################\n"
<< "Main Menu\n"
<< "Enter your command!\n"
<< "##############################\n"
<< endl
<< "1. Program1\n"
<< "2. Program2\n"
<< "0. Exit\n"
<< endl
<< "Command >> ";
cin>> option;
cout<< endl;
return option;
}
I'm not sure what your question is, but your code is missing 2 important things. First, you need break statements at the end of each case block, otherwise the program flow will continue on to the next case statement.
Second, the inner menu doesn't ever escape the inner while(1) loop. This is a possible case for a goto use, although in practice it would better to refactor the code to split the top menu and inner menu into two functions, and use a return in the inner menu to return to the outer menu.
I'm not sure what your question is, but your code is missing 2 important things. First, you need break statements at the end of each case block, otherwise the program flow will continue on to the next case statement.
Second, the inner menu doesn't ever escape the inner while(1) loop. This is a possible case for a goto use, although in practice it would better to refactor the code to split the top menu and inner menu into two functions, and use a return in the inner menu to return to the outer menu.
As said, you're code is missing various things. It would be awesome if you distribute the entire code, and additionally the exact error message with line.
void value not ignored as it ought to be?...
...Is not that much of an explanation...
Also, are you sure you included iostream?
#include iostream
That said, you did not declare any of the variables used in the program.
You also missed a space in line 2 of your mainMenu() function.
Also, please tell us what you expected to happen.
I have made a calculator( code below ) and it seems to run smoothly, except for 6 C4244 errors which don't seem to effect the way it runs. The only problem that I have now is that after you select any case, the program runs that part of the case and exits, but I want it to return to the main switch after it runs the case selected.How can that be done and why am i getting the errors?
void main(void)
{
int c;
printf("Enter the number of the type of operation you would like to perform:\n\n");
printf("1.Basic Mathematical Operations\n2.Trignometric Operations\n3.Quadratic Operations\n4.Lograthimic Operations\n5.Matrix Operations\n\nNumber selected: ");
scanf_s("%d",&c);
switch(c)
{
case 1:
int m;
printf("\n\nEnter the number of the operation you would like to perform:\n");
printf("1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Exponent\n6.Square Root\n7.Inverse\n\nNumber selected: ");
scanf_s("%d",&m);
switch(m)
{
case 1:
add();
break;
case 2:
sub();
break;
case 3:
mul();
break;
case 4:
div();
break;
case 5:
exp();
break;
case 6:
sqrt();
break;
case 7:
inv();
break;
}
break;
}
One of the C4244 errors is in the following function,in the line "r=sin(a*Pi/180.0);" and the function is called in another nested switch case:
void sind(void)
{
float a, r;
printf("enter angle");
scanf_s("%f",&a);
r=sin(a*Pi/180.0);
printf("The sine of %f degrees is %f.\n", a, r );
_getch();
}
Your conversion error of double to float is most likely due to the use of 180.0 which is automatically interpreted as double. You have to add an 'f' as suffix to specify a float like this 180.0f. Also you have to pay attention to your declaration of 'Pi' which should also be a float number.
You asked two questions, so I don't know which one is relevant to your post. However, I will answer the one where you are having trouble getting the code to go back to the beginning to request subsequent choices to the user.
The easiest way to accomplish the loop is to break up your code into functions. Having switch statements that are so many lines long is not maintainable, and the logic gets all lost.
Also, the main() function returns int, not void.
For example:
#include <iostream>
using namespace std;
int promptAndGetOperation()
{
int op;
cout << "Enter the number of the type of operation you would like to perform:\n\n";
cout << "1.Basic Mathematical Operations\n2.Trignometric Operations\n3.Quadratic "
"Operations\n4.Lograthimic Operations\n5.Matrix Operations\n\n"
"6. Exit\n\nNumber selected: ";
cin >> op;
return op;
}
void performOperation(int op)
{
// fill this in
}
int main(void)
{
int operation;
do
{
operation = promptAndGetOperation();
if ( operation >= 1 && operation <= 5)
performOperation(operation);
else
if ( operatiom < 1 )
cout << "Illegal operation. Try again\n\n";
}
while (operation != 6 );
}
This is a basic skeleton of what you should be doing. See how simple the main program becomes? The do-while loop is the main driver.
I am trying to get a vector to store objects of class 'Complex'.
This is how I have tried to get it to store:
ifstream values;
values.open("h://values.txt");
if(!values)
{
cout<<"Error: cannot open "<<"values.txt"<<endl<<endl;
}
else
{
//Initialise list
vector<Complex> v;
Complex *c1;
double a,b,d=0,e=0;
char c;
int count=0;
while(values)
{
values>>c>>a>>b;
c1=new Complex;
v.push_back(*c1);
cout<<c<<" "<<a<<" "<<b<<endl;
switch (c)
{
case 'r':
case 'R':
case 'p':
case 'P':
{
//Call constructor
v[count].setType(c);
switch (c)
{
case 'r':
case 'R':
{ v[count].setReal(a);
v[count].setImaginary(b);
v[count].topolar(a,b,d,e);
break;
}
case 'p':
case'P':
{ v[count].setLength(a);
v[count].setAngle(b);
v[count].frompolar(d,e,a,b);
break;
}
default:
{ cout<<"Type Error"<<endl;
break;
}
}
count++;
break;
}
default:
{
//error message
cout<<" Failed input type, ensure all of type 'r' or 'p'"<< endl;
cout<<"Programme Closing"<<endl;
break;
}
};
}
While this will read the info in my programme, it insists on reading the last line twice (I put the cout into this loop so it was easier to see what numbers were where). I have tried using a for loop, but because I want it to run till the end of file I think I have to use a while loop, but I might be wrong.
My supervisor said something along the lines of c1 being overwritten on every loop, but I thought this should be OK on the basis the information is passed to the vector before it is overwritten by the next line so I'm a bit confused.
The next problem is that when I then try and print out all the information again, outside of the loop shown above (for example to allow for manipulation before printing) it prints the same thing over and over until the for loop reaches the count...
int y;
int z;
while(y!=3)
{
cout<< " What would you like to do?"<<endl;
cout<< " Type the number of the option you would like"<<endl;
cout<< " 1. Show all numbers in polar form"<<endl;
cout<< " 2. Show all numbers in rectangular form"<<endl;
cout<< " 3. Show all numbers in both forms"<<endl;
cout<< " 4. Convert a number to its conjugate"<<endl;
cout<< " 5. Exit"<<endl;
cin>>y;
switch(y)
{
case 1:
for(z=0; z<count;z++)
{
v[z].getLength();
v[z].getAngle();
cout<< a<<" "<<b<<endl;};
break;
case 2:
for (z=0; z!=count;z++)
{
v[z].getReal();
v[z].getImaginary();
};
break;
case 3:
cout<<" Real Imaginary | Length Angle | Original Type"<<endl;
for(z=0; z!=count;z++)
{ v[z].getLength();
v[z].getAngle();
v[z].getReal();
v[z].getImaginary();
cout<<a<<" "<<b<<" "<<d<<" "<<e<<endl;
In case any of you try to run the programme:
#include "Class definitions.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string.h>
#include <cmath>
#include <vector>
using namespace std;
Any help will be greatly appreciated!!
Thanks muchly!
H
x
Whether input succeeds is only known after the input, not before. After you do
values>>c>>a>>b;
you use the values, even if the input has failed (e.g. because of end of file). Put the test in the condition of the while loop:
while ( values >> c >> a >> b )
and your code should work. (Whether this is the best way to handle the problem is another question. I'd probably use std::getline(), followed by std::istringstream to parse the line I'd read. Much easier error recovery.)
I've been messing around with my very basic C++ knowledge (been programming for two days), attempting to write a program that calculates user input around phi (1.61803399).
Here's the code, apologies if its a mess:
#include <iostream>
#include <math.h>
using namespace std;
//Prototypes:
float phiExpo;
float phiNegExpo;
float opt1f(float phi, float userInput){
return userInput * phi;}
float opt2f(float phi, float userInput){
return userInput / phi;}
float opt3f(){
return phiExpo;}
float opt4f(){
return phiNegExpo;}
float phiExpof(float phi, float userInput){
pow(phi,userInput);}
float phiNegExpof(float phi, float userInput){
pow(phi,-userInput);}
//Execute program:
int main(){
float userInput;
int userChoice;
float phi = 1.61803399;
float phiExpo;
float phiNegExpo;
cout<<"I want to (press corresponding number, then enter):"<<endl;
cout<<endl;
startchoices:
cout<<"1. Multiply by phi:"<<endl;
cout<<"2. Divide by phi:"<<endl;
cout<<"3. Exponentiate phi:"<<endl;
cout<<"4. Negatively exponentiate phi:"<<endl;
cout<<endl;
cin>>userChoice;
cout<<endl;
switch (userChoice){
case 1:
cout<<"Enter number for multiplication: ";
cin>>userInput;
return opt1f(phi, userInput);
case 2:
cout<<"Enter number for division: ";
cin>>userInput;
return opt2f(phi, userInput);
case 3:
cout<<"Enter number for to exponetiate phi by: ";
cin>>userInput;
return opt3f();
case 4:
cout<<"Enter number for negatively exponentiate phi by: ";
cin>>userInput;
return opt4f();
default:
cout<<"Please enter a number from 1 to 4.";
cout<<endl;
cout<<endl;
goto startchoices;
}
cin.get();
}
Anyway, upon entering a number at the first prompt (1-4), the program simply crashes to the desktop, and I can't figure out why.
Any help would be much appreciated.
Are you sure it crashes? The code simply returns the value of the operation (casted to an int, as this is the return type of main). I would suggest that you should print it using cout << opt4f().
The problem comes with the return statements in your switch.
The function main() is special in that the return value tells the operating system whether the program was successful. If the return value of main() is 0, then everything worked fine. If it is nonzero, then there was an error.
In your code, you are returning the value of opt1f(phi, userInput), optef(phi, userInput), etc. These values are likely non-zero, thus telling the operating system that your program failed.
Your program doesn't close on the switch statement when I run it; I get your second text.
There are a few problems I see though:
First, as faster people have noted, your returns quit out of the application instead of outputting the answer.
Secondly, after you change your code to cout rather than return, you will want to put in a "break;" so you don't run the code in every condition after the current one.
Thirdly, you might want to change the goto into an input loop and add a quit option to your menu. This is more of a stylistic choice, but I think you will find that goto's in c/c++ are harder to debug in the future.
-edit: for formatting-
Well, assuming you wanted to be able to do more than one thing per program run, and to get rid of the goto, you could do something like:
boolean quitting = false;
do {
cout << "1) Menu item 1" << endl << "2) Quit" << endl;
cin.get(userchoice);
switch(userchoice) {
case 1:
cout << "Please enter input for option 1: ";
cin >> userInput;
cout << case1function(userInput);
break;
case 2:
quitting = true;
break;
default:
cout << "Please read menu" << endl;
}
}while (!quitting);
In this case, the returns exit the main() function, leading to a clean program termination.
The program doesn't crash, it exits because that's what it is supposed to do. The return statement means that the execution will exit the current function, and in case of the current function being main - it will exit the program.
The value of the return is the value which the program will return to the OS. If it's not 0 - the OS will think the program exited abnormally, but in your case - it's just the value of the calculation.