When runnig my project i see these problems. Does anyone know the solution?
'std array': too few template arguments.
cannot use this indirection on type 'std::array'.
too few arguments for class template "std::array".
I tried to remove using namespace std; into std::array or cli::array but it didn't helped a lot
#include "pch.h"
#include "windows.h"
#include <iostream>
#include <string>
using namespace System;
using namespace std;
class Dog
{
public:
Dog();
Dog(int initAge);
~Dog();
int GetAge();
void SetAge(int Age);
void GawGaw(string S);
private: int itsAge;
};
Dog::Dog()
{ }
Dog::Dog(int initAge)
{
itsAge = initAge;
}
Dog::~Dog()
{ }
int Dog::GetAge()
{ return itsAge;
}
void Dog::SetAge(int Age)
{ itsAge = Age;
}
void Dog::GawGaw(string S)
{ cout << S + " -> GawGaw \n";
}
int main(array<System::String ^> ^args)
{
cout << "\n My Favorite dogs \n";
Dog Bob;
cout << "\nBob is a dog who is ";
cout << Bob.GetAge() << " years old. \n";
Bob.SetAge(7);
cout << "No. Bob is a dog who is ";
cout << Bob.GetAge() << " years old. \n";
Bob.GawGaw("Bob");
Dog* Fox = new Dog(10);
cout << "\nFox is a dog who is ";
cout << Fox->GetAge() << " years old. \n";
Fox->SetAge(12);
cout << "No. Fox is a dog who is ";
cout << Fox->GetAge() << " years old. \n";
Fox->GawGaw("Fox");
Dog Rex(5);
cout << "\nRex is a dog who is ";
cout << Rex.GetAge() << " years old. \n";
Rex.SetAge(8);
cout << "No. Rex is a dog who is ";
cout << Rex.GetAge() << " years old. \n";
Rex.GawGaw("Rex");
Console::ReadLine();
return 0;
}
Related
The word "getline" is a red and underlined, which indicate an error:
#include <iostream>
using namespace std;
string ReadName()
{
string Name;
cout << "Please Enter Your Name: " << endl;
getline(cin, Name);
return Name;
}
void PrintName(string Name)
{
cout << "\nYour name is " << Name << endl;
}
int main()
{
PrintName(ReadName());
return 0;
}
The VS Gives an error regarding getline
The problem is it prints the full name but not the rest of the lines about the person.
Could someone, please guide me?
I do really appreciate your help!
auto itr = find(my_vec.begin(), my_vec.end(), search );
if(itr != my_vec.end())
{
std::cout << "Match found " << search << std::endl;
std::cout << "\nFull name: " << search << std::endl;
} else {
std::cout << "Match not found "<< std::endl;
}
There are a few style problems with your code:
No need to explicitly initialize strings, they will be empty by default (see here).
Keep a consistent style. For example, either always start brackets in the same line as the function signature or in the next line.
No need to close the file explicitly at the end of the function, this is done when the object goes out of scope (see (destructor) here).
No need to include <map> and <iomanip> headers.
Don't keep unused variables.
Give suggestive names to your variables.
Do not return error codes to the OS when the app is working as it should. Not finding a name is not an error, is it?
It seems your file has 6 entries per contact, so all you have to do is print 5 more lines. You do not need to store the lines in a vector, just parse and print them as you go. Here is an example:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <fstream>
void findContact(std::string fullName, std::string contactListPath) {
std::ifstream inFile{contactListPath};
if (!inFile) {
std::cerr << "File could not be open!" << std::endl;
return;
}
std::string line;
while (std::getline(inFile, line)) {
if (line == fullName) {
std::cout << "Match found: \n";
std::cout << "\nFull name: " << fullName;
std::cout << "\nAddress: " << (std::getline(inFile, line), line);
std::cout << "\nE-mail: " << (std::getline(inFile, line), line);
std::cout << "\nPhone: " << (std::getline(inFile, line), line);
std::cout << "\nBirthday: " << (std::getline(inFile, line), line);
std::cout << "\nNote: " << (std::getline(inFile, line), line) << std::endl;
return;
}
}
std::cout << "Match not found " << std::endl;
}
int main() {
std::string fullName;
std::string contactListPath;
std::cout << "Enter full name to search: ";
std::getline(std::cin, fullName);
std::cout << "Enter path to contact list: ";
std::getline(std::cin, contactListPath);
findContact(fullName, contactListPath);
return 0;
}
If every entry contains 6 lines. Then you can print all the lines starting from the line that you found like:
auto itr = find(my_vec.begin(), my_vec.end(), search );
if(itr != my_vec.end())
{
std::cout << "Match found " << std::endl;
// print the next 6 lines
for(int remaining = 6;remaining > 0 && itr!=my_vec.end(); itr++,remaining--) {
std::cout << *itr << std::endl;
}
} else {
std::cout << "Match not found "<< std::endl;
}
Hello Everyone! I have a Project due tomorrow and I accidently chosen a hard one and now im regretting it. Im trying to make digital voting system on a small scale just to represent my idea as I'm a 2nd semester student of BSCS. I made the entire code by myself. The first code I made was working code but the output was not as I desired. I wanted to change the Vote after casting the vote. But it wasnt changing it. This is the first code Below:
#include<iostream>
#include<conio.h>
using namespace std;
void VoteLIST();
void VoteReturn();
class NADRA
{
protected:
double nic[4] = { {42401335},{1},{2},{3} };
int knum[4] = { {123},{1},{2},{3} };
int vote[4] = { {0},{0},{0},{0} };
void voteCHANGE(int a, int b)
{
vote[a] = b;
}
};
class CNIC :protected NADRA
{
private:
//nic = <=
double cnic;
int KhandaanNUM;
public:
CNIC()
{
cnic = KhandaanNUM = 0;
}
CNIC(double a, int b)
{
cnic = a;
KhandaanNUM = b;
}
void SetDATA(double a, int b)
{
cnic = a;
KhandaanNUM = b;
}
double getCNIC()
{
return cnic;
}
int getKNUM()
{
return KhandaanNUM;
}
int voteSTATUS = 0;
bool check()
{
for (int loop = 0; loop < 4; loop++)
{
if (cnic == nic[loop] && KhandaanNUM == knum[loop] && (vote[loop] == 0))
{
voteSTATUS = vote[loop];
return true;
break;
}
}
return false;
}
void setVOTE(int c)
{
//vote[voteSTATUS] = c;
voteCHANGE(voteSTATUS, c);
}
void VoteReturn()
{
cout << voteSTATUS;
}
};
class VOTEmenu
{
private:
CNIC c1;
double Cnic;
int kNum;
public:
void GetData()
{
cout << "Enter CNIC: ";
cin >> Cnic;
cout << "Enter Khandaan Number: ";
cin >> kNum;
c1.SetDATA(Cnic, kNum);
}
void checkData()
{
if (c1.check() == true)
{
int choice;
system("cls");
VoteLIST();
cout << endl;
cout << "Please choose the Respective Party Number" << endl;
cin >> choice;
c1.setVOTE(choice);
system("cls");
cout << "You have successfully voted for Party Number: " << choice <<endl;
}
else
{
system("cls");
cout << "You are not Eligible to Cast Vote!" << endl;
cout << "Reasons:\n1) CNIC or Khandaan Number You entered may be Incorrect";
cout << "\n2) You may not be 18 Above\n3) You may have already casted the vote";
}
}
void Data2()
{
if (c1.check() == true)
{
system("cls");
cout << "Your CNIC Number: " << Cnic << endl;
cout << "Khandaan Number: " << kNum << endl;
cout << "You Have Voted for party number: "; c1.VoteReturn();
}
else
{
system("cls");
cout << "You are not Eligible to Cast Vote!" << endl;
cout << "Reasons:\n1) CNIC or Khandaan Number You entered may be Incorrect";
cout << "\n2) You may not be 18 Above\n3) You may have already casted the vote";
}
}
};
void main()
{
while (true)
{
VOTEmenu v;
int choice;
system("cls");
cout << "Welcome To Vote Menu!" << endl;
cout << "Please choose any one of the following" << endl;
cout << "1) Cast Vote\n2) Check Vote";
cout << endl;
cin >> choice;
v.GetData();
if (choice == 1)
{
v.checkData();
_getch();
}
else if (choice == 2)
{
v.Data2();
_getch();
}
else
{
cout << "Wrong DATA:" << endl;
}
}
}
void VoteLIST()
{
cout << "VOTE LIST" << endl;
cout << "1: PTI\n2:PMLN\n3:PPP\n4:MQM" << endl;
}
Now I tried to remake the code and added file stream. taking text from external source instead of making array inside the visual studio and assigning values. but now Im facing an error which says This declaration has no storage class or type specifier The code is below:
#include<iostream>
#include<conio.h>
#include <fstream>
using namespace std;
void VoteLIST();
void VoteReturn();
class NADRA
{
protected:
fstream Text;
int nic[2];
int knum[2];
int vote[2];
int count = 0;
int ccnic = 0;
int knumm = 0;
int votee = 0;
Text.open("Nadra.txt")
{
while (!Text.eof())
{
Nadra >> ccnic >> knumm >> votee;
nic[count] = ccnic;
knum[count] = knumm;
vote[count] = votee;
count++;
}
}
Text.close();
void voteCHANGE(int a, int b)
{
vote[a] = b;
}
};
class CNIC :protected NADRA
{
private:
//nic = <=
double cnic;
int KhandaanNUM;
public:
CNIC()
{
cnic = KhandaanNUM = 0;
}
CNIC(double a, int b)
{
cnic = a;
KhandaanNUM = b;
}
void SetDATA(double a, int b)
{
cnic = a;
KhandaanNUM = b;
}
double getCNIC()
{
return cnic;
}
int getKNUM()
{
return KhandaanNUM;
}
int voteSTATUS = 0;
bool check()
{
for (int loop = 0; loop < 4; loop++)
{
if (cnic == nic[loop] && KhandaanNUM == knum[loop] && (vote[loop] == 0))
{
voteSTATUS = vote[loop];
return true;
break;
}
}
return false;
}
void setVOTE(int c)
{
//vote[voteSTATUS] = c;
voteCHANGE(voteSTATUS, c);
}
void VoteReturn()
{
cout << voteSTATUS;
}
};
class VOTEmenu
{
private:
CNIC c1;
double Cnic;
int kNum;
public:
void GetData()
{
cout << "Enter CNIC: ";
cin >> Cnic;
cout << "Enter Khandaan Number: ";
cin >> kNum;
c1.SetDATA(Cnic, kNum);
}
void checkData()
{
if (c1.check() == true)
{
int choice;
system("cls");
VoteLIST();
cout << endl;
cout << "Please choose the Respective Party Number" << endl;
cin >> choice;
c1.setVOTE(choice);
system("cls");
cout << "You have successfully voted for Party Number: " << choice << endl;
}
else
{
system("cls");
cout << "You are not Eligible to Cast Vote!" << endl;
cout << "Reasons:\n1) CNIC or Khandaan Number You entered may be Incorrect";
cout << "\n2) You may not be 18 Above\n3) You may have already casted the vote";
}
}
void Data2()
{
if (c1.check() == true)
{
system("cls");
cout << "Your CNIC Number: " << Cnic << endl;
cout << "Khandaan Number: " << kNum << endl;
cout << "You Have Voted for party number: "; c1.VoteReturn();
}
else
{
system("cls");
cout << "You are not Eligible to Cast Vote!" << endl;
cout << "Reasons:\n1) CNIC or Khandaan Number You entered may be Incorrect";
cout << "\n2) You may not be 18 Above\n3) You may have already casted the vote";
}
}
};
void main()
{
while (true)
{
VOTEmenu v;
int choice;
system("cls");
cout << "Welcome To Vote Menu!" << endl;
cout << "Please choose any one of the following" << endl;
cout << "1) Cast Vote\n2) Check Vote";
cout << endl;
cin >> choice;
v.GetData();
if (choice == 1)
{
v.checkData();
_getch();
}
else if (choice == 2)
{
v.Data2();
_getch();
}
else
{
cout << "Wrong DATA:" << endl;
}
}
}
void VoteLIST()
{
cout << "VOTE LIST" << endl;
cout << "1: PTI\n2:PMLN\n3:PPP\n4:MQM" << endl;
}
Can Someone please help??? Tomorrow is my last date! I just want my program to take data from text, chech the data with user entered data, IF correct, it will proceed and show for vote menu. if not, It will give error etc.
I know I'm a very pathetic learner. But You guys are professional! I will learn alot from you. Please help me!
I am using zybook, and it requires for me to create a program that maintains the inventory of a car rental agency.
The three files for this program are:
car.h - Class declaration
car.cpp - Class definition
main.cpp - main() function and other functions as described
When I wrote the program, I am getting an error:
main.cpp:4:10: fatal error: Car.h: No such file or directory
4 | #include "Car.h"
| ^~~~~~~
compilation terminated.
car.cpp:3:10: fatal error: Car.h: No such file or directory
3 | #include "Car.h"
| ^~~~~~~
compilation terminated.
Here is my code:
#include <iostream>
#include <string.h>
#include <vector>
#include "Car.h"
#include <algorithm>
using namespace std;
void addCar(vector<Car>& cars);
//bool deleteCar(vector<Car>& cars);
bool updateCarCondition(vector<Car>& cars);
void displayCar(vector<Car>& cars);
void displayAllCars(vector<Car>& cars);
bool rentCar(vector<Car>& cars);
bool returnCar(vector<Car>& cars);
int main(){
char option;
vector<Car> cars;
while (true){
cout << "CAR RENTAL AGENCY MENU" << endl;
cout << "a - Add car to inventory" << endl;
cout << "d - Delete car by id from inventory" << endl;
cout << "u - Update car by id condition in inventory" << endl;
cout << "s - Display one car by id from inventory" << endl;
cout << "i - Display list of all cars in inventory" << endl;
cout << "c - Rent a car by id in inventory" << endl;
cout << "r - Return a car by id in inventory" << endl;
cout << "q - Quit" << endl;
cout << "Choose an option: " << endl;
cin >> option;
cin.ignore();
cout << endl;
switch(option){
case 'a': addCar(cars);
break;
//case 'd': deleteCar(cars);
//break;
case 'u': updateCarCondition(cars);
break;
case 's': displayCar(cars);
break;
case 'i': displayAllCars(cars);
break;
case 'c': rentCar(cars);
break;
case 'r': returnCar(cars);
break;
case 'q': break;
default: cout << "Please enter a valid option" << endl;
}
}
return 0;
}
void addCar(vector<Car>& cars){
int id, year;
string model, make, condition;
cout << "ADD CAR TO INVENTORY" << endl;
cout << "Enter an ID: " << endl;
cin >> id;
cout << "Enter the make: " << endl;
cin >> make;
cout << "Enter the model: " << endl;
cin >> model;
cout << "Enter the year: " << endl;
cin >> year;
cout << "Enter the condition (new, slighty_used, used): " << endl;
cin >> condition;
Car car(id, make, model, year, condition);
cars.push_back(car);
}
/*bool deleteCar(vector<Car>& cars){
cout << "REMOVE CAR FROM INVENTORY" << endl;
int id;
cout << "Enter the ID of the car to delete: " << endl;
cin >> id;
vector<Car>::iterator it;
for (int i = 0; i < cars.size(); i++){
if (cars.at(i).Getid() == id){
//Car const& const_car = cars.at(i);
//std::vector<Car>::iterator itr = std::find(cars.begin(), cars.end(), const_car);
it = cars.at(i);
cars.erase(it);
return true;
}
}
return false;
}*/
bool updateCarCondition(vector<Car>& cars){
cout << "UPDARE CAR CONDITION IN INVENTORY" << endl;
int id;
string condition;
cout << "Enter the ID of the car to update condition: " << endl;
cin >> id;
cout << "Enter the condition (new, slighty_used, used): " << endl;
cin >> condition;
for (int i = 0; i < cars.size(); i++){
if (cars.at(i).Getid() == id){
cars.at(i).Setcondition(condition);
return true;
}
}
return false;
}
void displayCar(vector<Car>& cars){
cout << "DISPLAY CAR IN INVENTORY" << endl;
int id;
cout << "Enter the ID of the car to display: " << endl;
cin >> id;
for (int i = 0; i < cars.size(); i++){
if (cars.at(i).Getid() == id){
cars.at(i).displayCar();
break;
}
}
}
void displayAllCars(vector<Car>& cars){
cout << "DISPLAY ALL CARS IN INVENTORY" << endl;
for (int i = 0; i < cars.size(); i++){
cars.at(i).displayCar();
}
}
bool rentCar(vector<Car>& cars){
cout << "RENT CAR IN INVENTORY" << endl;
int id;
cout << "Enter the ID of the car to rent: " << endl;
cin >> id;
for (int i = 0; i < cars.size(); i++){
if (cars.at(i).Getid() == id){
if (cars.at(i).Getrented()){
cout << "Car is already rented" << endl;
return false;
}
else if (cars.at(i).Getrented() == false){
cout << "Car has been successfully rented to you" << endl;
cars.at(i).toggleRented();
return true;
}
}
}
cout << "Car " << id << " not found in inventory" << endl;
return false;
}
bool returnCar(vector<Car>& cars){
cout << "RENT CAR TO INVENTORY" << endl;
int id;
cout << "Enter the ID of the car to return: " << endl;
cin >> id;
for (int i = 0; i < cars.size(); i++){
if (cars.at(i).Getid() == id){
if (cars.at(i).Getrented()){
cars.at(i).toggleRented();
cout << "Car returned successfully!!" << endl;
return true;
}
}
}
cout << "Car " << id << " not found in inventory" << endl;
return false;
}
This program is a nightmare, it wont even give me errors when ran, visual studios tells me nothing and i need some help
#include <iostream>
using namespace std;
class Textbook
{
private:
char *aPtr;
char *tPtr;
int yearPub;
int numPages;
char bookType;
public:
Textbook(char *, char *, int, int, char);
void display();
void operator=(Textbook&);
};
Textbook::Textbook(char*string = NULL, char*string2 = NULL, int ypub = 0, int npages = 0, char btype = 'X')
{
aPtr = new char[strlen(string) +1];
strcpy(aPtr, string);
tPtr = new char[strlen(string2) +1];
strcpy(tPtr, string2);
yearPub = ypub;
numPages = npages;
bookType = btype;
}
void Textbook::display()
{
cout << "The name of the author is: " << *aPtr << endl;
cout << "The Title of the book is: " << *tPtr << endl;
cout << "The year it was published is: " << yearPub << endl;
cout << "The number of pages is: " << numPages << endl;
cout << "The initial of the title is: " << bookType << endl;
return;
}
void Textbook::operator=(Textbook& newbook)
{
if(aPtr != NULL) //check that it exists
delete(aPtr);// delete if neccessary
aPtr = new char[strlen(newbook.aPtr) + 1];
strcpy(aPtr, newbook.aPtr);
if(tPtr != NULL) //check that it exists
delete(tPtr); // delete if neccessary
tPtr = new char[strlen(newbook.tPtr) + 1];
strcpy(tPtr, newbook.tPtr);
yearPub = newbook.yearPub;
numPages = newbook.numPages;
bookType = newbook.bookType;
}
void main()
{
Textbook book1("sehwag", "Programming Methods", 2009, 200, 'H');
Textbook book2("Ashwin", "Security Implementation", 2011, 437, 'P');
Textbook book3;
book1.display();
book2.display();
book3.display();
book3 = book1;
book2 = book3;
book1.display();
book2.display();
book3.display();
}
im not sure if the problem lies in the default constructor but that's about the only thing i could think of, but im not sure at all on how to fix it.
Problem is with the default-parameters in the constructor.
You can't do those kind of operations with NULL-pointers.
Textbook book3;
crashes your program.
Change:
cout << "The name of the author is: " << *aPtr << endl;
cout << "The Title of the book is: " << *tPtr << endl;
to:
cout << "The name of the author is: " << aPtr << endl;
cout << "The Title of the book is: " << tPtr << endl;
Also change:
aPtr = new char[strlen(string) +1];
strcpy(aPtr, string);
to:
if (string != NULL)
{
aPtr = new char[strlen(string) +1];
strcpy(aPtr, string);
}
else
{
aPtr = new char[1];
aPtr[0] = '\0';
}
and ditto for tptr and string2.
The reason you need this checking is because you have NULL as a default value for your two string inputs, so when you call the constructor with no arguments (as is the case with book3) these strings are just NULL pointers. Calling functions such as strlen or strcat with a NULL pointer will result in an exception as you have seen.
Ideally you should not be using C-style strings with C++ - use C++ strings instead - this will help to avoid problems such as the above.