i know the question looks confusing but truthfully it isn't like it seems.
im building a programme about drone ( sorry if it doesnt make sense)
Here from this code i get error "(line 116 )identifier "camrecord" is undefined " and "Compiler Error C2065
'(line 116) identifier' : undeclared identifier" although i already identify and declare them? how to solve this?
thank you
#include<iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string.h>
using namespace std;
void manualMode();
void defaultMode();
void Getdata(double & camrecord, double & Projectframerate);
void calculate(double & camrecord, double & Projectframerate, double & actualframerate);
int speed, timeTofly, operation;
int angle = 90;
int main() {
int mode = 0, part = 3;
double flyLevel;
int obstacleSensor = 0, timer = 3, AccelerometerSensor = 0, tempSensor = 25;
char startPause;
cout << "Drone Flying Technology" << endl;
do {
cout << "Choose Mode: (1)Default, (2)Manual \n";
cin >> mode;
if (mode == 1)
defaultMode();
if (mode == 2)
manualMode();
} while (mode == 0);
cout << "Choose how to fly: (1)Upper , (2)Lower , (3)Upper and Lower \n";
cin >> part;
switch (part) {
case 1:
case 2:
flyLevel = 0.5;
break;
case 3:
flyLevel = 1;
break;
}
if (obstacleSensor == 0) {
do {
cout << "Press (S) to Start,and swing the propeller." << endl;
cin >> startPause;
cout << "LED is On\n";
while (AccelerometerSensor != flyLevel) {
AccelerometerSensor++;
}
cout << "Flying session Started! Time Left:" << timeTofly << endl;
cout << "Eagle eye Operation Started!\n";
timeTofly = timeTofly / 2;
cout << "Crusing mode initiate Time Left:" << timeTofly << endl;
timeTofly = timeTofly / 2;
cout << "Free fly commenced Time Left:" << timeTofly << endl;
timeTofly = 0;
startPause = 'P';
} while ((startPause == 's') || (startPause == 'S'));
}
cout << "End!\nLED is Off";
return 0;
}
void manualMode() {
cout << "Enter drone speed (knot) \n";
cin >> speed;
cout << "Enter Time to fly: \n";
cin >> timeTofly;
cout << "Choose Operation: (1)Fully manual, (2)Normal Orientation, (3)Free orientation, (4)FPV racing, (5)All\n";
cin >> operation;
}
void defaultMode() {
int howtofly;
cout << "how to fly: (1)Circle, (2)Altitude Hold, (3)Free orientation";
cin >> howtofly;
switch (howtofly) {
case 1:
speed = 30;
timeTofly = 3;
break;
case 2:
speed = 40;
timeTofly = 3;
break;
case 3:
speed = 60;
timeTofly = 3;
break;
}
char name[25];
char id[5];
float m1, m2, m3, m4, m5;
ofstream outputFile("droneinfo.txt", ios::out);
cout << "Please enter object you want to record,drone id imei number and your 5 preferred video fps:\n";
cout << "\nPress <ctrl> + z to stop. \n";
while (cin >> id >> name >> m1 >> m2 >> m3 >> m4 >> m5)
{
outputFile << id << " " << name << " " << m1 << " " << m2 << " " << m3 << " " << m4 << " " << m5 << endl;
}
double fps = 0.0;
double Projectframerate = 0.0;
double actualframerate, out = 0;
Getdata(camrecord, Projectframerate);
calculate(camrecord, Projectframerate, actualframerate);
cout << "The actual frame rate is =" << actualframerate << " f/s\n";
}
void Getdata(double& camrecord, double& Projectframerate) {
cin >> camrecord;
cin >> Projectframerate;
cout << "Please enter\n Recording frame rate =" << camrecord << "f/s ,\ndesired frame rate="
<< Projectframerate << "f/s\n";
}
//-------------------//
void calculate(double& camrecord, double& Projectframerate, double& actualframerate) {
actualframerate = camrecord / Projectframerate;
}
//-------------------//
So a reference as an argument is used when you want to input a variable and modify it. Looking at void Getdata(double& camrecord, double& Projectframerate), you are saying that Getdata takes a camrecord and a projectframe rate, reads these value and modifies them, so that the caller can use the new values.
Looking at the code, it does not seem Getdata is actually reading the input values. So there is an inconsistency.
To be consistent with the implementation of the method, the method Getdata should not take any arguments - but only return them. Now, you cannot return two arguments, but you can pack them into a struct and return that:
struct Data
{
double camrecord;
double Projectframerate;
};
Data Getdata();
The definition of Getdata can then be:
Data getData()
{
Data data;
cin >> data.camrecord;
cin >> data.Projectframerate;
cout << "Please enter\n Recording frame rate =" << data.camrecord << "f/s ,\ndesired frame rate="
<< data.Projectframerate << "f/s\n";
return data;
}
Similarly can be done for calculate.
Related
I have coded a program that gets the information of employees of a company, when I run it in Dec C++, it does run without any errors, but when I run it in Visual Studio, it returns the following error :
Error C4703 potentially uninitialized local pointer variable 'data' used
and VS asks me to rewrite the pointer like this :
employ *data{};
instaed of : employ *data;
Can anyone please explain why does it happen? and What this {} means here? Is it a thing of C++ or VS?
#include <iostream>
using namespace std;
struct employ {
long int emp_num;
string fn;
string ln;
int work_days;
long int payday;
};
int main()
{
int n=0;
employ *data;
int act;
do {
cout << "___________________________________________________________________________________________________________";
cout << "\n\n\t\tWelcome to the EMPLOTASK!";
cout << "\n\t\tFor doing any of the commands, enter number of that command.";
cout << "\n\t\t_______________________________________________________________";
cout << "\n\n\t\t\ ADD & EDIT : ";
cout << "\n\t\t[1] Add new employees.";
cout << "\n\t\t[2] Edit an existing employee.";
cout << "\n\t\t[3] Delete an existing employee.";
cout << "\n\t\t[4] Print list of all employees.";
cout << "\n\t\t_______________________________________________________________";
cout << "\n\n\t\t\ ACTIONS : ";
cout << "\n\t\t[5] Sort based on their salary.";
cout << "\n\t\t[6] Search for an emplyee.";
cout << "\n\t\t[7] Calculate the average salary.";
cout << "\n\t\t[8] Show maximum and minimum sallary.";
cout << "\n\n\t\tWhat do you want to do?";
cin >> act;
if (act > 8)
cout << "Invalid request!" << endl;
switch (act)
{
case 1: {
cout << "\n\t\t_______________________________________________________________";
cout << "\n\n\t\t\'ADD NEW EMPLOYEES\'";
cout << "\n\t\tEnter number of the employees : ";
cin >> n;
data = new employ[n];
for (int i = 0; i < n; i++)
{
cout << "\n\t\t========== Employee number " << i + 1 << " ==========";
cout << "\n\t\tFirst name : ";
cin >> data[i].fn;
cout << "\t\tLast name : ";
cin >> data[i].ln;
cout << "\t\tEmployee's number : ";
cin >> data[i].emp_num;
cout << "\t\tDays of work : ";
cin >> data[i].work_days;
cout << "\t\tDaily rate : ";
cin >> data[i].payday;
}
cout << "\n\t\t=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=";
cout << "\n\t\tOperatuon's done successfully! =)";
cout << "\n\t\t=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl;
break;
}
case 2: {
cout << "\n\t\t_______________________________________________________________";
cout << "\n\n\t\t\'EDIT AN EMPLOYEE\'";
cout << "\n\t\tEnter the employee number : ";
int em_num;
cin >> em_num;
int yes = 0;
if (n == 0)
{
cout << "There's no employee!";
}
for (int i = 0; i < n; i++)
{
if (em_num == data[i].emp_num)
{
for (int j = 0; j < n; j++)
{
cout << "\n\t\t========== Edit Employee number " << i + 1 << " ==========";
cout << "\n\t\tFirst name : ";
cin >> data[i].fn;
cout << "\t\tLast name : ";
cin >> data[i].ln;
cout << "\t\tEmployee's number : ";
cin >> data[i].emp_num;
cout << "\t\tDays of work : ";
cin >> data[i].work_days;
cout << "\t\tDaily rate : ";
cin >> data[i].payday;
yes++;
}
}
}
if (yes == 0)
{
cout << "The entered employee number is invalid.";
}
break;
}
default:
break;
}
} while (act != 0);
return 0;
}
Regarding {}: uniform initialization(C++11). It enables you to initialize everything in the same way. It also provides better safety guarantees when it comes to narrowing conversions.
The following code shows some examples.
#include <iostream>
#include <string>
#include <vector>
struct Point
{
Point(float x, float y) : m_x{ x }, m_y{ y } {}
float m_x;
float m_y;
};
int main()
{
int a{ 0 }; // same as a{}
int b{ 1 };
// int c{ 2.0 }; // at least warning, often error
double d{ 3.0 };
char* pc{nullptr}; // same as pc{}
std::string s{ "C++" };
std::vector<int> vi{1, 2, 3, 4, 5};
Point p1{ 3.0f, 4.0f };
}
I'm having trouble passing the reentered optionPackageCode after the nested while loop completes (cin >> optionPackageCode;) back to the first while loop so that it can check the conditions with the updated code. No clue what's wrong.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
//Declaring variables
double basePrice = 0.0;
double subTotal = 0.0;
double associatedCosts = 0.0;
double finalPrice = 0.0;
int sub = 0;
int tries = 0;
string optionPackageCode = "";
//Populating arrays
string optionPackageCodeArray[5] = { "BB", "SP", "NP", "HE", "UC"};
string packageNameArray[5] = { "Base", "Sport", "Intermediate", "Luxury", "Custom"};
double packageCostArray[5] = { 1500.00,3250.00,4575.00,7500.00,5220.00 };
//Ask for input
cout << "Welcome! Please enter the base price and option package code for your vehicle!"
<< endl;
cout << fixed << setprecision(2);
cout << "Base Price: ";
cin >> basePrice;
cout << endl << "Option package code: ";
cin >> optionPackageCode;
//While loop
while (tries < 5 && optionPackageCodeArray[sub] != optionPackageCode)
{
tries += 1;
while (optionPackageCodeArray[sub] != optionPackageCode && sub < 5)
sub += 1;
//end while
cout << "Sorry, that code wasn't found in our database! Please try again: " << endl;
cin >> optionPackageCode;
} //end while
//Calculations and final output
if (optionPackageCodeArray[sub] == optionPackageCode)
{
subTotal = basePrice + packageCostArray[sub];
associatedCosts = subTotal * 0.15;
finalPrice = subTotal + associatedCosts;
cout << "The final cost for your vehicle with " << packageNameArray[sub] << " trim is $" << finalPrice << endl;
}
else
cout << "Sorry, you are out of tries...";
system("pause");
return 0;
}
I must write a program where the user can choose to practice with topic addition or topic multiplication that starts with a self-driven menu.
It must keep track of questions answered right, wrong and the number of questioned asked.
Which my current program is doing within each module(topic). Example Addition keeps track of the questions while the user is practicing Addition only and Multiplication does the same.
However, they are not being feedback to main, so they are not being added or displayed before the user can select another topic to practice or to exit the program.
Currently it is only to keeping track of the question (right /wrong/ total of questions) for each module (topic).
My goal is for the values to be passed to main and display the total number (right /wrong/ total of questions) before the user exits the program, but at the same time I must display the number of question in the Additional Topic and the Multiplication topic and provide a total.
Example Table of Addition, Multiplication and Totals ?
This is the code I have to start with. Can someone help me in how to code to return values of the (right /wrong/ total of questions) of the two topics and accomplish to display something like the table information.
******************************************************************************* /
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <string> // String managment funtions.
#include <iostream> // For input and output
#include <cmath> // For math functions.
#include <math.h>
#include <cstdlib>
using namespace std;
////////////////////////////////////////////////////////////////////////
// Implementing menu driven programs.
// Function Prototypes.
int menu();
void sums();
void products();
int main()
{
srand(time(0));
int option;
do {
option = menu();
switch (option) {
case 1: {
sums();
break;
}
case 2: {
products();
break;
}
default:
cout << "Program exit" << endl;
}
} while (option != 6);
return 0;
}
int menu()
{
cout << "Please select an option" << endl;
cout << "1) Practice with Addition " << endl;
cout << "2) Pratice with Multiplication " << endl;
cout << "3) Exit the program " << endl;
int option;
cin >> option;
return option;
}
void sums()
{
string keepgoing;
unsigned int quantity_total_questions = 0U;
unsigned int quantity_wrong_answers = 0U;
unsigned int quantity_correct_answers = 0U;
do {
const int minValue = 10;
const int maxValue = 99;
int y = (rand() % (maxValue - minValue + 1)) + minValue;
// cout<< " the random number is y "<< y << endl;
int x = (rand() % (maxValue - minValue + 1)) + minValue;
// cout<< " the random number is x "<< x << endl;
cout << "What is " << x << " + " << y << " =" << endl;
int answer;
cin >> answer;
if (answer == (x + y)) {
cout << "Great!! You are really smart!!" << endl;
++quantity_correct_answers;
++quantity_total_questions;
}
else {
cout << "Oh Sorry Try Again." << endl;
++quantity_wrong_answers;
++quantity_total_questions;
}
cout << "Right: " << quantity_correct_answers;
cout << " Wrong: " << quantity_wrong_answers;
cout << " Total Questions: " << quantity_total_questions << endl;
cout << "Do you want to play again? [enter y for yes or n for no]" << endl;
cin >> keepgoing;
} while (keepgoing == "y");
}
void products()
{
{
string keepgoing;
unsigned int quantity_total_questions = 0U;
unsigned int quantity_wrong_answers = 0U;
unsigned int quantity_correct_answers = 0U;
do {
const int minValueOne = 0;
const int maxValueOne = 9;
const int minValueTwo = 10;
const int maxValueTwo = 99;
int y = (rand() % (maxValueOne - minValueOne + 1)) + minValueOne;
// cout<< " the random number is y "<< y << endl;
int x = (rand() % (maxValueTwo - minValueTwo + 1)) + minValueTwo;
// cout<< " the random number is x "<< x << endl;
cout << " What is " << x << " x " << y << " =" << endl;
int answer;
cin >> answer;
if (answer == (x * y)) {
cout << "Great!! You are really smart!!" << endl;
++quantity_correct_answers;
++quantity_total_questions;
}
else {
cout << "Oh Sorry Try Again." << endl;
++quantity_wrong_answers;
++quantity_total_questions;
}
cout << "Right: " << quantity_correct_answers;
cout << " Wrong: " << quantity_wrong_answers;
cout << " Total Questions: " << quantity_total_questions << endl;
cout << "Do you want to play again? [enter y for yes or n for no]" << endl;
cin >> keepgoing;
} while (keepgoing == "y");
}
}
I would create a structure that contains the number of total answers and number of correct answers—the incorrect ones can be inferred—and then pass a reference to an instance of the structure to the respective sums() and products() functions.
Those functions can then populate the structure elements and when they return, your main function can read them out, knowing exactly how many questions were asked, how many were answered, or whatever other information you want to record and retrieve.
I'm learning C++ at school and in my opinion it's a beautiful language, but I have this annoying problem. In the text book it's written with FILE *text and scanf and printf, and I personally don't like it; I got used to cin and cout or with the << >> better say with the fstream.
So here is my problem:
I have to make an application that writes data in binary mode (I have done it on half of it but it doesn't write in binary mode for some reason)
After I write the city (orasul) the coordinates (x and y) I have to search for them and get those values. (Here I tried to use string.find) but I have to use seekg to search in "binary mode" and get those values separate in a structure.
If you guys can guide me somehow cause I am pretty lost here. And is there a way I can get the sizeof(struct) ?
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include <limits>
using namespace std;
struct oras {
std::string orasul;
int x;
int y;
} ora;
void functiaPrincipala();
void calculator(float coordonate_x1, float coordonate_y1, float coordonate_x2, float coordonate_y2);
void adaugaOras();
void stergeLocatie();
void repetare();
void main() {
functiaPrincipala();
}
void functiaPrincipala() {
// variabile
int obtiune;
// ofstream fisierOut;
// ifstream fisierIn;
cout << "1) Adauga localitate: " << endl;
cout << "2) Stergerea unei localitati existente: " << endl;
cout << "3) Stergerea tuturor localitatilor existente: " << endl;
cout << "4) Afisarea tuturor localitatilor existente: " << endl;
cout << "5) Calculul distantei a doua localitati: " << endl;
cout << "Introduceti obtiunea: " << endl;
cin >> obtiune;
switch (obtiune) {
case 1:
adaugaOras();
break;
case 2:
stergeLocatie();
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
getch();
}
void calculator(float coordonate_x1, float coordonate_y1, float coordonate_x2, float coordonate_y2) {
float rezultat;
rezultat = sqrt((coordonate_x2 * coordonate_x1) - (coordonate_x2 * coordonate_x1) + (coordonate_y2 * coordonate_y1) - (coordonate_y2 * coordonate_y1));
cout << "Distanta de la orasul 1 la orasul 2 este de: " << rezultat;
}
void adaugaOras() {
int n;
ofstream fisierOutt("textttt.txt", ios::app | ios::binary);
// fisierOutt.open("textttt.txt");
cout << "Cate orase doresti sa introduci: ";
cin >> n;
if (fisierOutt.is_open()) {
for (int i = 0; i < n; i++) {
cout << "Introdu numele orasului: ";
cin >> ora.orasul;
cout << "Introdu coordonatele x: ";
cin >> ora.x;
cout << "Introdu coordonatele y: ";
cin >> ora.y;
fisierOutt << ora.orasul << " " << ora.x << " " << ora.y << endl;
cout << endl << endl;
}
} else {
cout << "Nu am putut deschide fisierul";
}
fisierOutt.close();
cout << endl;
// repetare();
}
void stergeLocatie() {
}
void repetare() {
char obtiune;
cout << "Doriti sa mai adaugati ceva sau sa iesiti?(d/n)";
cin >> obtiune;
if (obtiune == 'd') {
functiaPrincipala();
} else {
exit;
}
}
Like I said in my comment, you can't really seek to a specific entry as all entries are of different sizes.
It can be solved by having a separate index file, where each index entry contains of the position of the entry in the real file. This way, when you need entry X you first seek in the index file to the correct position, read the position, and then use that to seek in the real data file.
This is how many DBM database-managers handle their data.
The entries in the index-file has to be fixed size, for example each entry in the index is of type std::ostream::pos_type, and you use write to write the index, and read to read it.
Check https://stackoverflow.com/a/15452958/2156678 . There asker wanted a way to do search (and update) on binary file and I proposed a simple way to achieve the same.
\a3.cpp(75): error C2563: mismatch in formal parameter list
I'm certain I'm passing the function checkout with 3 doubles, I don't know why I'm getting the error I am. Please help.
#include <iostream>
#include <cstdlib>
using namespace std;
const double peanut_PRICE = 1.80;
const double peanut_SHIP = 0.50;
const double BOOK_PRICE = 9;
const double BOOK_SHIP = 1.06;
const double MOVIE_PRICE = 13.99;
const double MOVIE_SHIP = 0.05;
double checkout (double myamountofbooks, double myamountofmovies, double mypoundsofpeanuts)
{
myamountofbooks = myamountofbooks * (BOOK_PRICE + BOOK_SHIP);
myamountofmovies = myamountofmovies * MOVIE_PRICE * (1 + MOVIE_SHIP);
mypoundsofpeanuts = mypoundsofpeanuts * (peanut_PRICE + peanut_SHIP);
return (myamountofbooks + myamountofmovies + mypoundsofpeanuts);
}
bool validUserImput (int whereUserWantsToGoNext)
{
if (whereUserWantsToGoNext > 50 || whereUserWantsToGoNext < 0)
return false;
else return true;
}
bool validUserImput (double whereUserWantsToGoNext)
{
if (whereUserWantsToGoNext > 50 || whereUserWantsToGoNext < 0)
return false;
else return true;
}
int main()
{
//===========================Declaration Statements==================================
double amountofbooks = 0;
double amountofmovies = 0;
double poundsofpeanuts = 0;
int whereUserWantsToGoNext = 0;
while (! (whereUserWantsToGoNext == 4) )
{
cout << "1. Books\n2. Peanuts\n3. Movies\n4. Checkout\n" << endl;
cin >> whereUserWantsToGoNext;
if (!validUserImput(whereUserWantsToGoNext)) cout << "INVALID IMPUT" << endl;
if (whereUserWantsToGoNext == 1){
cout << "Please enter your number of books";
cin >> amountofbooks;
if (!validUserImput(amountofbooks)) cout << "INVALID IMPUT" << endl;
}
if (whereUserWantsToGoNext == 3){
cout << "Now please enter the number of movies you've selected";
cin >> amountofmovies;
if (!validUserImput(amountofmovies)) cout << "INVALID IMPUT" << endl;
}
if (whereUserWantsToGoNext == 2) {
cout << "Please enter the weight(in pounds) of your peanuts";
cin >> poundsofpeanuts;
if (!validUserImput(poundsofpeanuts)) cout << "INVALID IMPUT" << endl;
}
if (validUserImput == 4) cout << "Total Cost is..." << checkout(amountofbooks, amountofmovies, poundsofpeanuts);
}
cin >> amountofbooks;
}
The problem is here:
if (validUserImput == 4) ...
validUserImput is a function, but you are not calling that function, you are trying to compare it to 4.
If you wanted to keep track of the number of valid inputs you received, you could instead add a new variable that you manually increment on every valid input.
The last if - you are comparing function pointer to an integer. try this:
if (validUserImput(3) == 4) cout << "Total Cost is..." << checkout(amountofbooks, amountofmovies, poundsofpeanuts);
I assume you want to display the result of the checkout function if the user selects 4. So you probably wanted to write:
if (whereUserWantsToGoNext == 4) cout << "Total Cost is..." << checkout(amountofbooks, amountofmovies, poundsofpeanuts) << endl;