function was not declared in this scope. What am I missing? - c++

my functions output_all, add_item, and remove_item are all getting this error from the main.cc file. What am I missing? I assumed this was pretty straight forward, but obviously I messed it up. I'll include my files below. Thank you in advance for any help!
main.cc
#include <iostream>
#include <string>
#include <cstdlib>
#include "basket.h"
using namespace std;
int main(){
int choice;
string item;
cout << "Would you like to: \n\n 1.See List \n 2.Add an Item \n 3.Remove an item \n 4.Exit \n\n Please Enter a number: ";
cin >> choice;
if (choice == 1){
output_all(cout);
}
else if (choice == 2){
cout << "Please enter the item you would like to add to the list: ";
cin >> item;
add_item(item);
}
else if (choice == 3){
cout << "Please enter the name of the item you would like to remove: ";
cin >> item;
remove_item(item);
}
else if (choice == 4){
exit(1);
}
else {
main();
}
return (0);
}
basket.h
#include <iostream>
#include <string>
using namespace std;
class basket{
public:
//function will output all of the items currently in the list
void output_all(ostream& fout);
//Function will allow user to add item to list
void add_item(const string& item);
//function will remove item from list after checking that item exists in the list
void remove_item(const string& item);
// accessor method to access the string name
string get_name();
private:
//string variable used for storing thr list
string name;
};
basket.cc
#include <iostream>
#include <string>
#include "basket.h"
using namespace std;
void basket::output_all(ostream& fout){
fout << name;
}
void basket::add_item(const string& item){
name = name + " " + item;
}
void basket::remove_item(const string& item){
int num = name.find(item);
name.erase(num, item.length());
}
string basket::get_name(){
return (name);
}

I can see output_all(), add_item(), remove_item() are non static member of basket class. So output_all(cout) should be called from an object like: basketObj.output_all(cout);

Well, you are calling functions like output_all, add_item, remove_item in your main. There's no such functions in your program. Hence the [obvious] error.
If you tried to call member functions of class basket, then you should have declared an object of type basket and used a completely different syntax (member access syntax). Your favorite C++ book will have plenty of explanations and examples of this.
BTW, it is illegal to manually call main function in C++. You have to learn to use cycles to achieve what you are trying to do with that recursive call to main.

Related

Is it possible to put multiple names in one string?

I just started learning C++ and I'm currently following a tutorial on YouTube.
I thought it was fun to make a very simple 'access' program. If I type in my name it says, "Welcome!" If I type in another name it says, "access denied". It worked perfectly fine, but then I wanted the program to say "Welcome!" to two different names. So, I wanted to add a second name in the string, but I couldn't figure out how to do that. I googled a lot but I couldn't find anything. In the end, I came to string name = ("Joe", "Sean");, but here, it was only valid for Sean. I just can't figure out how to put multiple names in one string and make them both work. I hope you can help me, here is my code:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
string name = ("Joe", "Sean");
string input;
cout << "What is your name?\nMy name is: ";
cin >> input;
if(input == name){
cout << "Welcome, "<< input <<"! ";
} else {
cout << "Access denied";
}
return 0;
}
This is a way to do it using a vector of strings, so you can adapt easily with more names :
#include <iostream>
#include <vector>
using namespace std;
void printMessage(string message)
{
std::cout << message << std::endl;
}
int main()
{
vector<string> names{"Joe", "Sean", "Paul"};
string input;
cout << "What is your name? " << endl;
cin >> input;
for (string name : names)
{
if (name == input)
{
printMessage("Welcome!");
return 0;
}
}
printMessage("Access Denied!");
return 0;
}
The problem is in the string variable "name". You need an array of strings, not a single string.
This is an example implementation:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
string names[] = {"Joe", "Sean"};
string input;
cout << "What is your name?\nMy name is: ";
cin >> input;
for (int i = 0; i < end(names) - begin(names); i++) {
if(input == names[i]){
cout << "Welcome, "<< input <<"! " << endl;
return 0;
}
}
cout << "Access denied" << endl;
return 0;
}
You encountered some quirky features of C++ in the approach you are using to initialize your string variable:
string s1 = ("Joe"); // creates a string "Joe"
string s2 = ("Joe", "Sean"); // creates 2 strings, "Joe" and "Sean", and the variable s2 stores only the latter!
For more details on the different methods for initializing variables there has been an interesting discussion in this previous question.

Passing a string by reference in c++

I am working on a code for my c++ class. The assignment is to read the names from 2 different txt files(already in my directory) and find if the string/name that the user searched for matches any of the names already in the files. My code seems good to me, but I am getting an error in my function prototype saying "string was not declared in this scope." Any solutions? My code is here as follows:
#include <fstream>
#include <string>
#include <vector>
void boysfunc(string&, string&);
void girlsfunc(string&, string&);
using namespace std;
int main()
{
vector<string> boysnames;
vector<string> girlsnames;
string boysname, girlsname;
ofstream outputFile;
cout << "Enter a boy's name, or N if you do not want to
enter a name: ";
cin >> boysname;
cout << "Enter a girl's name, or N if you do not want to
enter a name: ";
cin >> girlsname;
if (boysname != "N")
{
boysfunc(boysname, boysnames);
}
if (girlsname != "N")
{
girlsfunc(girlsname, girlsnames);
}
}
void boysfunc(string &boysname, string &boysnames)
{
outputFile.open("BoysNames.txt");
while(outputFile >> boysnames)
{
/*Declare local variable count to use as a counter*/
int count = 0;
if (boysnames(count) == boysname)
{
outputFile.close();
cout << "The name " << boysname << " is very
popular among boys.";
return;
}
else
{
count++;
}
}
}
void girlsfunc(string &girlsname, string &girlsnames)
{
outputFile.open("GirlsNames.txt");
while(outputFile >> girlsnames)
{
/*Declare local variable count to use as a counter*/
int count = 0;
if(girlsnames(count) == girlsname)
{
outputFile.close();
cout << "The name " << boysname << " is very
popular among girls.";
return;
}
else
{
count++;
}
}
}
There are two major errors that you need to fix here.
using namespace std; must be written before the use of strings if you wish to omit std:: before writing string. Otherwise, you can write std::string& in the function declarations.
boysfunc() and girlsfunc() are taking vector<string>& as the second argument, whereas you incorrectly mentioned string& in the functions' declaration and definition. Fix that.
In this snippet
string s = "hello";
using namespace std;
the type string is not known to the compiler. That's what using namespace std; does. It basically turns string into std::string.
You could swap the 2 lines above, and it will work, but I highly recommend just saying std::string explicitly everywhere. I'm sure your IDE will let you do this easily.

My c++ program terminated without taking the input.What should i do?

The code below,works fine but it does not take any value for age and terminates.`
#include <iostream>
#include <string>
using namespace std;
class user{
int id,level=1,kills=0,age;
char name[20],server[40];
public:
void get(){
cout<<"Enter your name:";
cin>>name[20];
cout<<"Enter your age:";
cin>>age;
}
};
int main(){
user u;
u.get();
return 0;
}
/*Output
Enter your name:Jack
Enter your age:
C:\Users\user\documents\c++
*/
In the output section ,age is not accepted and the program terminates.
Use string name instead of char name[20] to take multi-character value. char name[20] will terminate after taking a single character.
Also, its valued will not be displayed on giving output.
Modified code for reference.
#include <iostream>
#include <string>
using namespace std;
class user{
int id,level=1,kills=0,age;
string name,server;
public:
void get(){
cout<<"Enter your name:";
cin>>name;
cout<<"Enter your age:";
cin>>age;
}
//test output
void put(){
cout<<name<<endl;
cout<<age<<endl;
}
};
int main(){
user u;
u.get();
//test
u.put();
return 0;
}
Just modify the code to this :
#include <iostream>
#include <string>
using namespace std;
class user{
int id,level=1,kills=0,age;
char name[20],server[40];
public:
void get(){
cout<<"Enter your name:";
cin>>name; // changes done here
cout<<"Enter your age:";
cin>>age;
}
};
int main(){
user u;
u.get();
return 0;
}
Job Done :)
Your problem is here:
cin>>name[20];
Why:
'name[20]' is 21th char of the array you defined before. It counts from 0! As this, it is simply a single char. If you now enter more than a single char, the rest is read by the cin>>age.
Example:
cout<<"Enter your name:";
cin>>name[20];
cout<<"Enter your age:";
cin>>age;
std::cout << "Name " << name << std::endl;
std::cout << "Age " << age << std::endl;
And entering:
Enter your name:1234
Enter your age:Name
Age 234
As you see, the '1' is now in the name and the rest is stored in age.
But attention: You defined your array as `name[20], which means you have 0..19 elements. Accessing name[20] is wrong!
But what you simply want to do was:
cin >> name;
The easiest way to handle strings (a long sequence of characters) or even the strings that have spaces just use the following library in C++.
#include <bits/stdc++.h>
Then just declare a string variable.
String name;
Now you can save a very long string without any error. e.g.
name = jshkad skshdur kslsjue djsdf2341;
and you'll get no error, enjoy ;)

How to gather multiple strings and input them into an array

In c++, is it possible for me to make a loop, that asks the user to input information, and loop back to ask if they would like to keep adding information, until they dont. And store the values they typed into different parts of an array?
Ive been trying to use a while loop, but what happens is it will prompt if they want to add info, they select yes, they add it, if no, it puts into the terminal what they typed, but if I do yes twice, I just overwrite the first value, any way to correct this? I am quite new to programming and would appreciate any help, thank yo. (What I have is just a template to go off of, I know why it doesnt work, I also left out the preprocessor directives
#include <iostream>
#include <string>
using namespace std;
int main()
{
string add;
string name [100];
while (true)
{
cout <<"Would you like to add a name? Yes or No? \n";
getline(cin, add);
if (add == "yes" or add == "Yes")
{
cout<<"Enter a name\n";
getline(cin, name);
}
else if( add == "no" or add == "No")
{break;}
else
{
cout <<"sorry, that is not a valid response"<<endl<<endl;
}
}
cout<<name;
return 0;
}
I think trying to use pointers could work, but they have been confusing me haha. Any help or advice would be greatly appreciated, thank you.
Use of std::vector can make your life easier here. See a code snippet:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string add;
string name;
vector<string> names;
while (true)
{
cout <<"Would you like to add a name? Yes or No? \n";
getline(cin, add);
if (add == "yes" or add == "Yes")
{
cout<<"Enter a name\n";
getline(cin, name);
names.push_back(name);
}
else if( add == "no" or add == "No")
{
break;
}
else
{
cout <<"sorry, that is not a valid response"<<endl <<endl;
}
}
// printing contents
for(auto n:names)
cout << *n <<endl;
return 0;
}
Use array Name with Index instead of variable name when store information like this
#include <iostream>
#include <string>
using namespace std;
int main()
{
string add;
string name [100];
int index = 0; //Index Location of Array
while (true)
{
cout <<"Would you like to add a name? Yes or No? \n";
getline(cin, add);
if (add == "yes" or add == "Yes")
{
cout<<"Enter a name\n";
getline(cin, name[index]); // write name with index to insert value in particular index
}
else if( add == "no" or add == "No")
{break;}
else
{
cout <<"sorry, that is not a valid response"<<endl<<endl;
}
}
//loop here to output all value of name as mention above
cout<<name;
return 0;
}

C++ getline won't work with int?

I've been working on this project for my C++ class in which I have to get a collection of information from the user, including the users birthyear and the current year (we have yet to learn how to access the computer's date so it has to be manually retrieved). I'm still fairly early in the process and I've run into this obstacle that I can't seem to work around.
While I've used this process to easily get the name system to work using the custom class, I can't get it to work for the year value. I can only assume the issue is that the year is an int rather than a string, but I can't possibly figure out any other way to get this to work. Could someone please look at this code and help me figure out what the issue is?
Main class:
#include <iostream>
#include <string>
#include "Heartrates.h"
using namespace std;
int main() {
Heartrates myHeartrate;
cout << "Please enter your name (First and Last): ";
string yourName;
getline (cin, yourName);
myHeartrate.setName(yourName);
cout << "\nPlease enter the current year: ";
int currentYear;
getline (cin, currentYear);
myHeartrate.setCyear(currentYear);
cout << "\nYou entered " << currentYear;
}
Heartrate class:
#include <string> //enables string use
class Heartrates {
public:
void setName(std::string yourName) {
name = yourName;
}
std::string getName() const {
return name;
}
void setCyear(int currentYear) {
Cyear = currentYear;
}
int getCyear() const {
return Cyear;
}
private:
std::string name;
int Cyear{ 0 };
};
I keep running into an error that states there is no matching overload function found, yet as you can see I'm using the same structure between both the main class and the header and the name worked just fine.
The version of std::getline you are trying to use there does not accept an int as a argument. See the function marked 2 (C++11) for the function you are trying to call here. std::istringstream can be included from sstream. I would add a std::endl (or new line) to the final print out to make it appear nicer as well.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
Heartrates myHeartrate;
cout << "Please enter your name (First and Last): ";
// read line
string line;
getline (cin, line);
// line is yourName
myHeartrate.setName(line);
// read line, read int from line
cout << "\nPlease enter the current year: ";
int currentYear;
getline (cin, line);
std::istringstream ss(line);
ss >> currentYear;
myHeartrate.setCyear(currentYear);
cout << "\nYou entered " << currentYear << endl;
return 0;
}