Simple C++: How to globalise variables in C++ - c++

I have an error when I try to build my program reading:
'error: 'celsius()' was not declared in this scope'
Now, correct me if I'm wrong but I think the problem is that because the function 'fahrenheit' comes before my other function 'celsius' when I call it in the fahrenheit function, it won't work. Now, it would be simple enough to switch them around but fahrenheit is also called in the celsius function.
In python, all you need to do is just globalise it with the 'global' syntax so what is the C++ equivalent?
Thanks
PS. Here is my code if you want it.
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int fahrenheit(){
system("CLS");
cout << "-----------------------------------------------";
cout << "\nYOU HAVE CHOSEN FAHRENHEIT TO CELSIUS MODE";
cout << "\n----------------------------------------------";
bool again;
again = true;
while (again == true){
int tempurf;
cout << "\nFahrenheit Temperature to be Converted: ";
cin >> tempurf;
int tempurc;
tempurc = tempurf - 32;
tempurc = tempurc * 5;
tempurc = tempurc / 9;
cout << "\n\n" << tempurf << " F is " << tempurc << " C";
cout << "\n\n\n\nWHAT WOULD YOU LIKE TO DO: ";
cout << "\n - ANOTHER CONVERSION TYPE A";
cout << "\n - FOR CELSIUS MODE TYPE C";
cout << "\n - TO EXIT TYPE E";
bool goodc;
goodc = false;
while (goodc == false){
string choosing;
cout << "\n ";
cin >> choosing;
if (choosing == "A" or choosing == "a"){
system("CLS");
goodc = true;
}
else if (choosing == "C" or choosing == "c"){
goodc = true;
again = false;
celsius();
}
else if (choosing == "E" or choosing == "e"){
goodc = true;
again = false;
return 0;
}
else{
cout << "\n Invalid Choice";
}
}
}
}
int celsius(){
system("CLS");
cout << "---------------------------------------------";
cout << "\nYOU HAVE CHOSEN CELSIUS TO FAHRENHEIT MODE";
cout << "\n---------------------------------------------";
bool again;
again = true;
while (again == true){
int tempuc;
cout << "\nCelsius Temperature to be Converted: ";
cin >> tempuc;
int tempuf;
tempuf = tempuc * 9;
tempuf = tempuf / 5;
tempuf = tempuf + 32;
cout << "\n\n" << tempuc << " C is " << tempuf << " F";
cout << "\n\n\n\nWHAT WOULD YOU LIKE TO DO: ";
cout << "\n - ANOTHER CONVERSION TYPE A";
cout << "\n - FOR FAHRENHEIT MODE TYPE F";
cout << "\n - TO EXIT TYPE E";
bool goodc;
goodc = false;
while (goodc == false){
string choosing;
cout << "\n ";
cin >> choosing;
if (choosing == "A" or choosing == "a"){
system("CLS");
goodc = true;
}
else if (choosing == "F" or choosing == "f"){
goodc = true;
again = false;
fahrenheit();
}
else if (choosing == "E" or choosing == "e"){
goodc = true;
again = false;
return 0;
}
else{
cout << "\n Invalid Choice";
}
}
}
}
int main(){
cout << "Welcome to the Fahrenheit/Celsius Converter!";
cout << "\n By Ben Sarachi";
cout << "\n\nWhich way would you like to convert to:";
cout << "\n - If you would like Fahrenheit to Celsius please type F";
cout << "\n - If you would like Celsius to Fahrenheit please type C";
// GC stands for good choice
bool gc;
gc = false;
while (gc == false){
string choice;
cout << "\n ";
cin >> choice;
//Call Functions
if (choice == "F" or choice == "f"){
gc = true;
fahrenheit();
}
else if (choice == "C" or choice == "c"){
gc = true;
celsius();
}
else{
cout << "Invalid Choice";
}
}
}

You want to add a forward declaration for your function so that the compiler knows the function exists. What's happening is that Fahrenheit is calling Celsius, but the compiler doesn't know what Celsius is at that point.
At the top of your code, add the following just below your includes:
int fahrenheit();
int celsius();
This tells the compiler that you will be defining those functions at some point.
Then you can declare your functions in any order in the file that you like.
Also, for future reference, that forward declaration should have the same signature as your function. So if you had a function like:
void foo(int bar) { ... }
then your forward declaration would be:
void foo(int);

What you need is function forward declarations. Put the following string before definition of fahrenheit function:
int celsius();
This will tell the compiler, that such a function exists and has the following prototype. But the body will be introduce at some point later.

You get the error because at the time of compiling the fahrenheit() function, celsius() is not known. You have to forward declare it.
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int celsius(); // this is the forward declaration
int fahrenheit(){
// do something
celsius();
}
int celsius(){
// implement the function
}
The alternative is you create a class and put the two functions as member of that class. You won't need forward declaration then (though declaring a class is arguably another form of that).
You have several other problems in your code:
your functions are set to return int, but not all paths return a value
what is or here choice == "F" or choice == "f"? is it #defined somewhere as ||?
do not use conditions such as gc == false or gc == true. prefer to use gc and !gc as in if(gc) or while(!gc)

Separate function declaration from definition.
Change your code to something like this:
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
int celsius();
int fahrenheit();
int fahrenheit()
{
// ...
}
int celsius()
{
// ...
}
int main(int argc, char** argv)
{
// ...
}

Related

How to Accept [ENTER] key as an invalid input and send out error message

This is a program that grade user inputs for the questions of Driver's License Exam.
I'm having trouble of validating the user input.
I'd like to accept the [ENTER] key as an invalid input and proceed to my validation rather than just go to an empty line and cannot process to the next question. Purpose is to send out error message and that no input is given and [ENTER] key is not valid input and only accept one more chance to enter valid input which are a/A, b/B, c/C, or d/D. So that is why I'm using if statement here instead of loop.
I tried if (testTakerAnswers[ans] == (or =) '\n') {} but still doesn't solve the problem of newline.
I include curses.h in here hope to use getch() statement from the other post but somehow I can't manage to work in my code with an array instead of regular input.
I'm looking for other methods as well rather than getch()
So should I adjust my bool function, or directly validate input in main() function.
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <curses.h>
using namespace std;
const unsigned SIZE = 20; // Number of qns in the test
char testTakerAnswers[SIZE]; //Array to hold test taker's answers
bool validateInput(char);
class TestGrader
{
private:
char answers[SIZE]; // Holds the correct answers // Answer is array
int getNumWrong (char[]);
void missedQuestions (char[]);
public:
void setKey(string); // Initialize object with standard keys
void grade(char[]); // Grades the answers from tester
};
void TestGrader::setKey(string key){
if (key.length()!=SIZE){
cout << "Error in key data.\n";
return;
}
for (unsigned pos = 0; pos < SIZE ; pos ++)
answers [pos] = key [pos];
}
void TestGrader::grade(char test[])
{
int numWrong = getNumWrong(test);
if (numWrong <= 5)
cout << "Congratulations. You passed the exam.\n";
else
cout << "You did not pass the exam. \n";
cout << "You got " << (SIZE-numWrong) << " questions correct. \n";
if (numWrong > 0){
cout << "You missed the following " << numWrong << " questions: \n";
missedQuestions(test);
}
}
int TestGrader::getNumWrong(char test[])
{
int counter = 0;
for (int i = 0; i < SIZE; i++){
if (answers[i] != toupper(testTakerAnswers[i])){
counter++;
}
}
return counter;
}
void TestGrader::missedQuestions(char test[])
{
// cout << testTakerAnswers[i]; This is to print taker's answers
int counter = 0;
for (int i = 0; i < SIZE; i++){
if (answers[i] != toupper(testTakerAnswers[i])){
cout << "\n" << i + 1 << ". Correct answers: " << answers[i];
counter++;
}
}
}
bool validateInput(char ans){ // Only A, B, C, D valid input
if (toupper(ans)!='A' && toupper(ans)!= 'B' && toupper(ans)!='C' && toupper(ans)!= 'D'){
cout << "\n********************WARNING*******************\n";
cout << "Invalid input! Enter only a/A, b/B, c/C, or d/D\n";
return false;
}
if (testTakerAnswers[ans] == '\n'){
return false;
}
return true;
}
int main()
{
const int NUM_QUESTIONS = 20;
string name; //Test taker's name
char doAnother; //Control variable for main processing loop
TestGrader DMVexam; //Create a TestGrader object
DMVexam.setKey("BDAACABACDBCDADCCBDA");
do {
cout << "Applicant Name: ";
getline(cin,name);
cout << "Enter answer for " << name << ".\n";
cout << "Use only letters a/A, b/B, c/C, and d/D. \n\n";
for (int i = 0; i < NUM_QUESTIONS; i++){
// Input and validate it
do{
cout << "Q" << i+1 << ": ";
cin >> testTakerAnswers[i];
if (!validateInput(testTakerAnswers[i])){
cout << "You get one more chance to correct.\nOtherwise, it count as wrong answer.";
cout << "\n*********************************************";
cout << "\nRe-enter: ";
cin >> testTakerAnswers[i];
cout << '\n';
break;
}
}while(!validateInput(testTakerAnswers[i]));
}
//Call class function to grade the exam
cout << "Results for " << name << '\n';
DMVexam.grade(testTakerAnswers);
cout << "\nGrade another exam (Y/N)? ";
cin >> doAnother;
while (doAnother != 'Y' && doAnother != 'N' && doAnother != 'y' && doAnother != 'n'){
cout << doAnother << " is not a valid option. Try Again y/Y or n/N" << endl;
cin >> doAnother;}
cout << endl;
cin.ignore();
}while(doAnother != 'N' && doAnother != 'n');
return 0;
}
Your issue is cin >> testTakerAnswers[i]; cin is whitespace delimited, that means that any whitespace (including '\n') will be discarded. So testTakerAnswers[i] can never be '\n'.
I'm not sure exactly what you want to do, but possibly try
getline(cin,input_string);
then
input_string == "A" | input_string == "B" | ...
So if only the enter key is pressed, input_string will become "".

problems with conditional statement based off sizeof c++

I am new to c++ and and am working on a program that has is a simple dvd rental program. I am having issues with case 3 & 4 specifically. Maybe I am misunderstanding the purpose behind sizeof. What I am trying to have it do is tell if the char array is empty and if it is allow the user to check it out by putting their name in and if it is not available give them a response saying that it is not available. case 4 should do the opposite and allow them to check it in. Any suggestions would be greatly appreciated.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <limits>
using namespace std;
const int arrSize = 5;
struct dvdStruct //distance struct
{
int id;
char title[51] = { 0 };
char rating[5] = { 0 };
double price;
char borrower[51] = { 0 };
} dvd;
dvdStruct dvds[arrSize] = {};
int userSelection; //intput variable for main menu selection
int borrowId (0);
int borrowIdReturn(0);
//void initalize();
int main() {
int size(0);
dvds[0].id = 1;
dvds[1].id = 2;
dvds[2].id = 3;
dvds[3].id = 4;
dvds[4].id = 5;
strcpy(dvds[0].title, "Fast 1");
strcpy(dvds[1].title, "Fast 2");
strcpy(dvds[2].title, "Fast 3");
strcpy(dvds[3].title, "Fast 4");
strcpy(dvds[4].title, "Fast 5");
strcpy(dvds[0].rating, "PG - 13");
strcpy(dvds[1].rating, "PG - 13");
strcpy(dvds[2].rating, "PG - 13");
strcpy(dvds[3].rating, "PG - 13");
strcpy(dvds[4].rating, "PG - 13");
dvds[0].price = '19.1';
dvds[1].price = '19.2';
dvds[2].price = '19.3';
dvds[3].price = '19.4';
dvds[4].price = '19.5';
strcpy(dvds[0].borrower, "");
cout << strlen(dvds[0].borrower) << endl;
strcpy(dvds[1].borrower, "\0");
strcpy(dvds[2].borrower, "\0");
strcpy(dvds[3].borrower, "\0");
strcpy(dvds[4].borrower, "\0");
do {
cout << "1.Display All DVD’s" << endl << "2.Display DVD Detail" << endl << "3.Check Out a DVD" << endl << "4.Check In a DVD" << endl << "5.Exit" << endl;
cin >> userSelection; //Input from the user.
switch (userSelection)
{
case 1:
for (int i = 0; i < arrSize; i++)
{
std::cout << dvds[i].title << "' " << dvds[i].rating << " " << dvds[i].borrower << endl;
}
system("pause");
system("CLS");
break;
case 2:
int dvdNum;
cout << "Enter a DVD number:";
cin >> dvdNum;
std::cout << dvds[dvdNum - 1].title << "' " << dvds[dvdNum - 1].rating << endl;
system("pause");
system("CLS");
break;
case 3:
cout << "Enter and id:";
cin >> borrowId;
if (strlen(dvds[borrowId-1].borrower) == 0)
{
cout << "Enter your name: ";
cin >> dvds[borrowId-1].borrower;
}
else
{
cout << "This dvd is not available" << endl;
}
system("pause");
system("CLS");
break;
case 4:
cout << "Enter and id:";
cin >> borrowIdReturn;
if (strlen(dvds[borrowIdReturn - 1].borrower) == 0)
{
cout << "This dvd is available" << endl;
}
else
{
cout << "Your DVD has been returned " << endl;
strcpy(dvds[borrowIdReturn - 1].borrower, "\0");
}
system("pause");
system("CLS");
break;
case 5:
return 0;
break;
}
} while (userSelection == 1 || userSelection == 2 || userSelection == 3 || userSelection == 4);
}
sizeof() gives you the size of an object. The size of the object is always the same, no matter what's in the object. In fact, sizeof() is calculated at compile time, and its value could not be affected, in any way, by whatever happens at runtime.
C++ code should use std::string, instead of char arrays, in most cases. std::string's empty() method indicates whether the string is empty.
If you still insist on working with C-style char arrays, and C-style '\0' terminated strings, use the C strlen() function to check if the character array contains nothing but a leading '\0', indicating an empty string.

I keep getting the error "The variable 'b' is being used without being initialized, and I'm not sure how to fix it

//Benjamin McKinney
//CSCI 2010-10
//Spring 2015
//PASS 3
//Programmed on Windows 8.1 using Visual C++ 2010 Express
//This program plays the game MasterMind
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
struct Player
{
string Name;
int HighScores[6];
bool CheatOn;
};
struct Board
{
int NumHoles;
int Holes[6];
};
struct Guess
{
int Count;
int NumHoles;
int Holes;
};
void printHighScores(string);
void readHighScore(string);
void updateHighScore(string, int);
string getPlayer();
int getBoard();
void playGame(string);
void menu(string);
int main()
{
Player p;
srand((unsigned int)time(0));
cout << "!!! Benjamin McKinney's Master-MasterMind!!!\n";
cout << "--------------------------------------------\n";
getPlayer();
menu(p.Name);
cout << "Goodbye, " << p.Name << endl;
printHighScores(p.Name);
cout << "----------------------------------------------\n";
cout << "!!! Benjamin McKinney's Master-MasterMind!!!\n";
system("PAUSE");
return 0;
}
void printHighScores(string name)
{
return;
}
void readHighScore(string)
{
return;
}
void updateHighScore(string, int)
{
return;
}
string getPlayer()
{
Player p;
cout << "What is your name?\n";
cin >> p.Name;
cout << "Welcome, " << p.Name << endl;
p.CheatOn = false;
readHighScore(p.Name);
return p.Name;
}
int getBoard()
{
Board b;
cout << "Enter the number of holes you would like: ";
cin >> b.NumHoles;
if(b.NumHoles > 6 || b.NumHoles < 1)
{
cout << "Error! You must pick a number between 1 and 6! Try again!\n";
}
for(int i=0;i<b.NumHoles;i++)
{
b.Holes[i] = rand() % 2 + 1;
}
return b.NumHoles;
}
void playGame(string)
{
Player p;
Board b;
Guess g;
getBoard();
g.Count=0;
for(int i=0;i<b.NumHoles;i++)
{
cout << "Enter your guess for the row\n";
if(p.CheatOn == true)
{
for(int a=0;a<(sizeof(b.Holes)-1);a++)
{
cout << b.Holes[a];
}
}
cout << "Enter your guess for hole " << i << ": ";
cin >> g.Holes;
g.Count++;
}
return;
}
void menu(string)
{
Player p;
char choice;
cout << "Please choose an option below:\n";
cout << "\t P)lay\n\t Q)uit\n\tChoice: ";
cin >> choice;
if(choice == 'P')
playGame(p.Name);
else
if(choice == 'Q')
return;
else`enter code here`
if(choice == 'C')
{
p.CheatOn = true;
playGame(p.Name);
}
}
Ignore the three HighScore functions, but otherwise I can't get this to work... "Run-Time Check Failure #3 - The variable 'b' is being used without being initialized." is the main issue that I'm having. If anyone can help me I would really appreciate it. Thanks!
In the playGame function:
void playGame(string)
{
Player p;
Board b; // <----- uninitialized
// ...
for(int i=0;i<b.NumHoles;i++)
// ^^^^^^^^^^
you use b.NumHoles when you have never initialized b.
I guess you intended that getBoard() would magically have some effect on b but it doesn't. The getBoard function updates a local board but never does anything with it.
To fix this you could change getBoard to return the whole board:
Board getBoard()
{
Board b;
// set up b...
return b;
}
and then inside playGame:
Board b = getBoard();
There's another error just below:
for(int a=0;a<(sizeof(b.Holes)-1);a++)
The sizeof operator gives the size in bytes. You actually want the size in elements, so you need to divide by the element size:
a < (sizeof b.Holes / sizeof b.Holes[0])
I'm not sure what the -1 was meant to be doing either, this would just cause you to not output the last hole.

c++ compiler ignoring first if statement

I am a newby at this and am working on my fist if/else program. I am having trouble getting the first if statement to recognize my input of "r". I tried playing with just one statement at a time I was able to input all the examples of input the teacher gave us with the desired output for residential and business. However when I run the program altogether I have a problem. I select R for residential, 0 for additional connections, 0 for premium channels and instead of output of $18.50 I get the business fee of $75.00. I am sure it is a simple mistake but I can't figure out what I am doing wrong. Can someone who knows how to work an if/else give me some insight on this!
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const float BASIC_RESIDENTIAL = 18.50;
const float BASIC_BUSINESS = 75.00;
const float CONNECT_RESIDENTIAL = 6.50;
const float CONNECT_BUSINESS = 5.00;
const float PREMIUM_RESIDENTIAL = 7.50;
const float PREMIUM_BUSINESS = 50.00;
char customerType;
int numOfConnections;
int numOfPremiumChannels;
float amountCableBill;
cout << fixed << setprecision(2);
cout << "Residential or Business [R or B]? ";
cin >> customerType;
cout << endl << endl;
cout << "How many Additional Connections? ";
cin >> numOfConnections;
cout << endl << endl;
cout << "Total number of Premium Channels: ";
cin >> numOfPremiumChannels;
cout << endl << endl;
if (customerType == 'R' || customerType == 'r')
{
amountCableBill = BASIC_RESIDENTIAL + CONNECT_RESIDENTIAL * numOfConnections + PREMIUM_RESIDENTIAL * numOfPremiumChannels;
}
//else customerType == 'B' || customerType == 'b'; // unnecessary
{
if (numOfConnections <= 9)
amountCableBill = BASIC_BUSINESS + PREMIUM_BUSINESS * numOfPremiumChannels;
else
amountCableBill = BASIC_BUSINESS + (numOfConnections - 9) * CONNECT_BUSINESS + PREMIUM_BUSINESS *numOfPremiumChannels;
}
cout << "Total amount of Cable Bill: " << amountCableBill << endl << endl;
cout << "Press <ENTER> to end..." << endl;
_getch();
return 0;
}
While the condition else if (customerType == 'B' ...) may be redundant, you still have to put an else before the opening brace of the branch.
It's
if (condition) { code } else { code }
You need else in the condition (unless you want "some other code" to be executed every time)
if (customerType == 'R' || customerType == 'r')
{
//Some Code
}
else //<--Notice else
{
//Some other code.
}

c++ multiplication game branching statement issue

So I am working on this math console based game. To work on the chapters of this book which I am reading.
I want to be able to make the game quit after using the no option.
The Problem is that when using the no option the program will cycle through once and then quit. I want the program to quit immediately.
I tried adding an else option but it keeps giving me the error code: "(26): error C2181: illegal else without matching if"
Also could anyone tell me how I could add the switch class to add more menus to the game. Would this require more function prototypes?
Thank you for all of your help stack overflow, I'm still learning how to use branching statements!
// multiplicationgame.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
void game();
int _tmain(int argc, _TCHAR* argv[])
{
char choice = 0;
game();
while(choice != 'n')
{
cin >> choice;
if (choice == 'y')
cout << "\n\n";
game();
//else
//cout << "later";
//
}
return 0;
}
void game()
{
srand(time(NULL));
int a = rand() % 23;
int b = rand() % 23;
int c = (a * b);
int d = 0;
char choice = 0;
cout <<"What does " << a << " * " << b << " equal?" << endl << endl << endl;
cout << "\n";
while(d != c)
{
if(d != c)
{
cout << "Please enter a number: ";
cin >> d;
}
}
cout << "\n\nCorrect! " << (a * b) << " is the answer!" << endl << endl;
cout << "Play again (Y) or (N)?" << endl;
}
Looks like you're missing some braces. Change this block…
if (choice == 'y')
cout << "\n\n";
game();
…to this…
if (choice == 'y')
{
cout << "\n\n";
game();
}
Also, it would probably be better to change this statement…
while(choice != 'n')
{
…
}
…to this…
while(choice == 'y')
{
…
}
This way, only 'y' will be considered a confirmation. If it is the other way, anything other than 'n' will be considered a confirmation.