#include <iostream>
using namespace std;
int main ()
{
int Age =25;
string name = "John";
cout << Age name ;
return 0;
}
compilation
In function 'int main()':
10:13: error: expected ';' before 'name'
For C++, you need all the other important parts, such as the include files, and the namespace qualifiers.
Unlike most scripting languages, the programming statements and expressions have to be in the context of a function.
For example:
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
int main()
{
int Age = 25;
string name = "John";
cout << Age << " " << name << endl;
return 0;
}
The error says that there's a missing semicolon somewhere, your compiler should tell you in which line is the error and you might find it.
Also you should do all the previous steps in the c++ code as following :
#include <iostream>
using namespace std;
int main()
{
int age = 25;
string name= "john";
cout<<"The age is: "<<age<<endl;
cout<<"The name is: "<<name<<endl;
return 0;
}
if there's still a problem please clarify it.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int Age ;
Age = 32;
string name ;
name = "ahmed";
cout << "age is"<< Age << " " <<endl;
cout<< "name is " << name << endl;
return 0;
try this
#include <iostream>
using namespace std;
int main(){
int Age =25; string name = "John";
cout<< name << " age ="<<Age;}
you mean that
Related
I have a banking project and I am trying to set up the bank name, address, and working hours. My getlines are showing an error as well as my get functions.
Input exact error messages here please.
'getline': no matching overloaded function found
no suitable user-defined conversion from "Bank" to "std::string" exists
Here's the class for bank:
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cctype>
#include <cstdlib>
using namespace std;
class Bank {
public:
void setBankName(string bn) { bn = bankname; }
string getBankName() { return bankname; }
void setBankAdd(string ba) { ba = bankadd; }
string getBankAdd() { return bankadd; }
void setWorkingHours(string bwh) { bwh = bankworkinghours; };
string getWorkingHours() { return bankworkinghours; }
private:
string bankname, bankadd, bankworkinghours;
};
//and then this is in my main function
int main() {
Bank bankname, bankadd, bankworkinghours;
char userChoice; // numbers 1-9
int number=0;
system ("color 5f");
cout << "Name of bank: ";
getline(cin, bankname); **//all the get lines also show error**
cout << endl;
cout << "Bank address: ";
getline(cin, bankadd);
cout << endl;
cout << "Bank working hours: ";
getline(cin, bankworkinghours);
cout << endl;
bankname.setBankName(bankname); //the things in the parentheses show error
bankadd.setBankAdd(bankadd);
bankworkinghours.setWorkingHours(bankworkinghours);
The error is self explanatory. 2nd parameter of getline function is std:string so define bankname as std:string and then set the name of bank object by setBankName.
1- You did not created bank Object in the main to set class attributes.
You need an Object with reference to that object you will set the parameters of the class bank.
2- bankname, bankadd, bankworkinghours are string and you made them Bank
Here is updated code and working fine in VS 2019 without any error. Just a few changes in the first 2 and last three lines of main
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cctype>
#include <cstdlib>
using namespace std;
class Bank {
public:
void setBankName(string bn) { bn = bankname; }
string getBankName() { return bankname; }
void setBankAdd(string ba) { ba = bankadd; }
string getBankAdd() { return bankadd; }
void setWorkingHours(string bwh) { bwh = bankworkinghours; };
string getWorkingHours() { return bankworkinghours; }
private:
string bankname, bankadd, bankworkinghours;
};
//and then this is in my main function
int main() {
Bank bankObj;
string bankname, bankadd, bankworkinghours;
char userChoice; // numbers 1-9
int number = 0;
system("color 5f");
cout << "Name of bank: ";
getline(cin, bankname);
cout << endl;
cout << "Bank address: ";
getline(cin, bankadd);
cout << endl;
cout << "Bank working hours: ";
getline(cin, bankworkinghours);
cout << endl;
bankObj.setBankName(bankname);
bankObj.setBankAdd(bankadd);
bankObj.setWorkingHours(bankworkinghours);
}
void setBankName(string bn) { bn = bankname; } is the wrong way around. try bankname = bn.
When I compile this I get the error title before my name functions in Name.cpp. I've never seen this error before. What's confusing is that there's already a constructor before the three functions in Name.cpp, since that's what the error seems to be talking about.
main.cpp
#include <iostream>
#include <string>
#include "Name.h"
using namespace std;
int main()
{
}
Name.h
#ifndef NAME_H
#define NAME_H
#include <iostream>
#include <string>
using namespace std;
class Name
{
public:
Name();
string getFirst(string newFirst);
string getMiddle(string newMiddle);
string getLast(string newLast);
void setFirst();
void setLast();
void setMiddle();
private:
string First;
string Middle;
string Last;
};
#endif // NAME_H
Name.cpp
#include "Name.h"
#include <iostream>
#include <string>
using namespace std;
Name::Name()
{
}
Name::setFirst(newFirst){
Name = newFirst;
cout << "You entered: " << Name << endl;
}
Name::setMiddle(newMiddle){
Middle = newMiddle;
cout << "You entered: " << Middle << endl;
}
Name::setLast(newLast){
Last = newLast;
cout<< "You entered: " << Last << endl;
}
You cannot omit type names of arguments. Write ones. Also function prototypes in declaration and definition have to be matched.
Your Name.h should be
#ifndef NAME_H
#define NAME_H
#include <iostream>
#include <string>
using namespace std;
class Name
{
public:
Name();
string getFirst();
string getMiddle();
string getLast();
void setFirst(string newFirst);
void setLast(string newLast);
void setMiddle(string newMiddle);
private:
string First;
string Middle;
string Last;
};
#endif // NAME_H
and Name.cpp should be
#include "Name.h"
#include <iostream>
#include <string>
using namespace std;
Name::Name()
{
}
void Name::setFirst(string newFirst){
Name = newFirst;
cout << "You entered: " << Name << endl;
}
void Name::setMiddle(string newMiddle){
Middle = newMiddle;
cout << "You entered: " << Middle << endl;
}
void Name::setLast(string newLast){
Last = newLast;
cout<< "You entered: " << Last << endl;
}
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
struct student
{
string::name(20)
string::fathername(20)
int rollno;
float cgpa;
{
student s1,s2;
s1.name ='ali';
s1.fathername='akram';
s1.rollno=1;
s1.cgpa=2.2;
cout<<s1.name<<endl;
cout<<s1.fathername<<endl;
cout<<s1.rollno<<endl;
cout<<s1.cgpa<<endl;
getch();
}
this program give me error when i use parameter in program this program give me thre error when use parameter why?
#include<iostream>
#include<string>
using namespace std;
int main()
{
struct student
{
string name;
string fathername;
int rollno;
double cgpa;
};
student s1, s2;
s1.name = "ali";
s1.fathername = "akram";
s1.rollno = 1;
s1.cgpa = 2.2;
cout << s1.name << endl;
cout << s1.fathername << endl;
cout << s1.rollno << endl;
cout << s1.cgpa << endl;
getchar();
return 0;
}
Your code has got a several errors. First of all I do not see a reason why you added , second important thing is that you should format your code, instead of using string::name(20); simply use string name;,third is that you have to put ; when you are ending a struct.
just a beginner student learning basic C++. I'm trying to figure out the best way to:
Turn a char array Name of 20 into a string that can be printed.
I found in other Stack Overflow topics to use "str()" such as "str(Name)", but it always comes up 'identifier not found'.
cout << "Name:" << str(Name) << endl;
Set a char array of 20 characters. For some reason, the following gives me errors when declaring. I've tweaked it so many times, but I cannot get why it won't give.
TESCStudent.Name[20] = {'S','u','p','e','r','P','r','o','g','r','a','m','m','e','r','\0'};
Full code I have so far:
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
//Step 1
struct StudentRecord
{
char Name[20];
//Accessor
void printInfo() const;
};
void StudentRecord::printInfo() const
{
cout << "Name:" << str(Name) << endl;
}
int main()
{
//Step 2
StudentRecord TESCStudent;
TESCStudent.Name[20] = {'S','u','p','e','r','P','r','o','g','r','a','m','m','e','r','\0'};
//Step 3
TESCStudent.printInfo();
_getch();
return 0;
}
Given that you are at a very beginner level, just use std::string:
#include <iostream>
#include <conio.h>
#include <string>
struct StudentRecord {
std::string Name;
void printInfo() const {
std::cout << "Name:" << Name << '\n';
}
};
int main() {
StudentRecord TESCStudent;
TESCStudent.Name = "SuperProgrammer";
TESCStudent.printInfo();
_getch();
}
Live demo
The syntax like this:
char Name[20] = {'S','u','p','e','r','\0'};
is used to initialize a variable when you define it. However, in your case,
StudentRecord TESCStudent;
TESCStudent.Name[20] = ...;
You've already defined it on the line before, so you can't "initialize", you have to "assign" it.
This is pretty much why you use std:string instead of char[].
I'm new to C++, could someone please explain to me why I received the below errors when I do use "std::getline"? Here's the code:
#include <iostream>
#include <string>
int main() {
string name; //receive an error here
std::cout << "Enter your entire name (first and last)." << endl;
std::getline(std::cin, name);
std::cout << "Your full name is " << name << endl;
return 0;
}
ERRORS:
te.cc: In function `int main()':
te.cc:7: error: `string' was not declared in this scope
te.cc:7: error: expected `;' before "name"
te.cc:11: error: `endl' was not declared in this scope
te.cc:12: error: `name' was not declared in this scope
However, the program would run and compile when I used "getline" with "using namespace std;" instead of std::getline.
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
cout << "Enter your entire name (first and last)." << endl;
getline(cin, name);
cout << "Your full name is " << name << endl;
return 0;
}
Thank you!
The errors are not from std::getline. The error is you need to use std::string unless you use the using namespace std. Also would need std::endl.
You need to use std:: on all the identifiers from that namespace. In this case, std::string and std::endl. You can get away without it on getline(), since Koenig lookup takes care of that for you.
#include <iostream>
#include <string>
int main()
{
std::string name; // note the std::
std::cout << "Enter your entire name (first and last)." << std::endl; // same here
std::getline(std::cin, name);
std::cout << "Your full name is " << name << std::endl; // and again
return 0;
}
You just needed to state the namespace for various elements that are in the std namespace (alternatively, you can remove all the std::s and place a using namespace std; line after your includes.)
Try this:
#include <iostream>
#include <string>
int main()
{
std::string name;
std::cout << "Enter your entire name (first and last)." <<
std::endl;
while(getline(std::cin, name))
{
std::cout <<"Your name is:"<< name << '\n';
}
return 0;
}