Hi I have a code that's look's like this
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name, classes;
cout << "Enter your name:" << endl;
getline(cin, name);
cout << "Enter your class:" << endl;
getline(cin, classes);
cout << "your name is " << name << "and your class is in " << classes << endl;
cout << "\n\n";
system("pause");
return 0;
}
and while I compile it compiled successfully without any warning or error message but, when I try to run the scripts it's suddenly disappear. how can I solve this problem? please someone help me
I recently use g++ version 9.2.0
Related
I have just started C++ after working with C for almost a year. I'm writing a program for a user to input info about a song. I read that I should use getline() to read strings with spaces. Here is my code:
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
typedef struct Song
{
char title[20];
char album[20];
char artist[20];
int year;
} song;
//store song info
Song Input;
char inputStr[20];
int inputYear;
cout << "Enter the name of a song: ";
getline(cin, inputStr);
strcpy(Input.title, inputStr);
cout << "Enter the album of your song: ";
getline(cin, inputStr);
strcpy(Input.album, inputStr);
cout << "Enter the artist of your song: ";
getline(cin, inputStr);
strcpy(Input.artist, inputStr);
cout << "Enter the year your song was released: ";
cin >> inputYear;
Input.year = inputYear;
//print
cout << "Song title: " << Input.title << endl;
cout << "From album: " << Input.album << endl;
cout << "Artist: " << Input.artist << endl;
cout << "Released: " << Input.year << endl;
return 0;
}
My compiler1 throws 3 errors, one for each of the getline() calls, not recognizing getline() despite the fact I have #include <string>. I have looked up sample usage of the getline() function.
Thanks for any help.
1I have wondered if this problem might concern an issue with the standard of C++ that my compiler supports. I did a bit of research and I did not find anything that helped me learn which standard I am using. Here's some info:
I'm using Terminal on Mac.
After g++ version:
Configured with: --prefix=/Applications/Xcode.app/Cont.../usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)
These lines seem to be the only ones of use here, but I could give more info. If someone has any idea which standard of C++ this is, whether it's C++11, or C++14, or otherwise, that would also be very helpful. Thanks again.
UPDATE:
started from scratch, tried to take as much of your advice as possible while still sticking to some of what I know. No errors and works just as I hoped. Thanks for all your help.
New code:
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
struct Song
{
string title;
string artist;
string album;
string year;
}song;
int main()
{
Song Input;
cout << "Song? ";
getline(cin, Input.title);
cout << "Artist? ";
getline(cin, Input.artist);
cout << "Album? ";
getline(cin, Input.album);
cout << "Year? ";
getline(cin, Input.year);
cout << "Song: " << Input.title << endl;
cout << "Artist: " << Input.artist << endl;
cout << "Album: " << Input.album << endl;
cout << "Year: " << Input.year << endl;
return 0;
}
The version of getline you are using takes a std::string as a parameter, not an array of char. If you want to use an array of char (and you shouldn't), you need to use the member function version:
cin.getline( some_char_array, array_size );
I would switch from using char arrays to using string's everywhere. For example I would do your code like this:
#include <string>
#include <iostream>
struct Song{
std::string title;
std::string album;
std::string artist;
int year;
};
int main()
{
//store song info
Song Input;
int inputYear;
std::cout << "Enter the name of a song: ";
getline(std::cin, Input.title);
std::cout << "Enter the album of your song: ";
getline(std::cin, Input.album);
std::cout << "Enter the artist of your song: ";
getline(std::cin, Input.artist);
std::cout << "Enter the year your song was released: ";
std::cin >> Input.year;
//print
std::cout << "Song title: " << Input.title << '\n';
std::cout << "From album: " << Input.album << '\n';
std::cout << "Artist: " << Input.artist << '\n';
std::cout << "Released: " << Input.year << std::endl;
return 0;
}
My preference is to not use using namespace std; but there's nothing wrong with it. Notice that using strings directly I don't need to copy things. I can use getline to do all that for me. I also don't need to worry about overrunning the size of the char array because string does that for me as well.
Hoping to free myself from Eclipse and not wanting to keep using the online cpp.sh, I wrote a small program in Cygwin in nano and tried to run it. The code is included for clarity.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i;
string mystr;
cout << "Enter number: ";
cin >> i;
cout << "You entered: " << i;
cout << " and its double: << i*2 << ".\n";
cin.ignore();
cout << "Name: ";
getline(cin, mystr);
cout << "Hello " << mystr << ".\n";
cout << "Team? ";
getline(cin, mystr);
cout << "Go " << mystr << "! \n";
return 0;
}
Trying to run it returns a series of errors, as seen in the picture. Right now, I'm trying to understand why "using" is not recognized. Checking Google found many similar complaints, but never about the command "using," probably because "using" is a common enough word to be using in a different context.
You can't run source code directly. You must compile it first.
Well, a lot of g++ compile errors are because you have
cout << " and its double: << i*2 << ".\n";
when you probably meant
cout << " and its double: " << i*2 << ".\n";
with one more quotation mark
As soon as you insert the " into line 14 it will compile and run, but the last portion will not complete.
I would also create another string near mystr, and use it for getline(). Reusing mystr faults when I compile it.
int main()
{
int i;
string mystr;
string team;
`getline(cin, team);
cout << "Go " << team << "! \n";`
After changing and compiling, I get:
Enter number: 3
You entered: 3 and its double: 6.
Name: Aaron
Hello Aaron.
Team? Meow
Go Meow!
I'm trying to use a basic char* and can manage to do it in main, but when try putting it in another function, and then calling that function in main, I'll sometimes get a bus error or segmentation fault, but I'm not sure why.
This code works the way I'd expect:
#include <iostream>
using namespace std;
int main(void){
cout << "enter name:" << endl;
char *name[10];
cin >> *name;
cout << "hello: " << *name << endl;
return 0;
}
The output is:
enter name:
alex
hello: alex
But when I do this:
#include <iostream>
using namespace std;
void sayhello(){
cout << "enter name:" << endl;
char *name[10];
cin >> *name;
cout << "hello: " << *name << endl;
}
int main(void){
sayhello();
return 0;
}
It compiles fine and the output gets to asking for the name, but I receive a buss error: 10. The output is:
enter name:
alex
Bus error: 10
Another issue I have is when I seem to do a very similar task, but do the same thing explicitly in main and add another function, I instead get a Segmentation fault: 11. My code is:
#include <iostream>
using namespace std;
void sayhello(){
cout << "enter name:" << endl;
char *newname[10];
cin >> *newname;
cout << "hello: " << *newname << endl;
}
void testprint(){
cout << "this is a test" << endl;
}
int main(void){
testprint();
cout << "enter name:" << endl;
char *name[10];
cin >> *name;
cout << "hello: " << *name << endl;
sayhello();
return 0;
}
My output for this is:
this is a test
enter name:
alex
enter name:
Segmentation fault: 11
It doesn't make any sense to me and I'm not sure why I'd be getting two different errors.
The line
char *name[10];
defines an an array of 10 char* objects that haven't been initialized. Perhaps, you meant to use:
char name[10]; // An array of 10 chars.
cin >> name;
Pointer to char is confusing for beginner. I would advise the following:
void sayhello(){
cout << "enter name:" << endl;
char name[10];
cin >> name;
cout << "hello: " << name << endl;
}
As said by Yu Hao, you have to learn more about pointer first.
I am writingthat code but gives me undefined reference error. Here is the error : undefined ref. yo display students , undefined reference to adCourse
#include <iostream>
#include "Course.h"
#include "Student.h"
#include "StudentReviewSystem.h"
using namespace std;
int main()
{
// create GradeBook object
Course s( 201, "CS");
cout << "name : " << s.courseName << endl;
cout << "ID : " << s.courseID << endl;
cout << "Enter student ID :" << endl;
int ID;
cin >> ID;
cout << "Enter student name :" << endl;
string name;
cin >> name;
s.addStudent(ID,name);
// cout << "You add the student with name: " << s.studentName << " and ID : " <<
cout << "Enter student ID :" << endl;
int ID2;
cin >> ID2;
cout << "Enter student name :" << endl;
string name2;
cin >> name2;
s.addStudent(ID2,name2);
s.displayStudents();
StudentReviewSystem ilknur;
ilknur.addCourse(111,"HUM");
cout << ilknur.courses[0].courseID;
return 0; // indicate successful termination
}
The Linker is telling you that your files has an external dependency it cannot resolve.
Probably you need to include the path to the soruce code (StudentReviewSystem.c) to your project paths. That's how you would solve it in Eclipe. VS adds everything it need when you add files to the project.
Every cpp file has to be compiled and linked in order to get an executeable. (If no external libraries are used this should do at least.)
If that hint does not help, please post your compiler and linker commands or the whole bild output.
I have been trying to test build this old 'text adventure' thing I found,
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
/////////////////////////////////////////////////////// VARIABLES //////////////////////////////////////////////////////////
string name;
string shipName;
int health;
int crewCount;
int armyTotal, activeArmy;
int casualtiesCount, woundedCount, healedCount;
// 'food' as in a whole meal (beverage, chewables, etc)
int foodCount;
////////////////////////////////////////////////////// INTRODUCTIONS ///////////////////////////////////////////////////////
cout << "What is thy name?\nName: ";
cin >> name;
cout << endl << "What will you name your ship?\nShip Name: ";
cin >> shipName;
cout << "\nSETTING: You are floating through space on giant space cruiser " << endl << "known as the " << shipName << ".\n You are on a random patrol sorti, just looking out for any trouble...";
cout << "Press ENTER to continue...";
cin.get();
cout << "\nFrom here on out, type corresponding number to which choice you want to make.\nPress ENTER to continue...";
cin.get();
//////////////////////////////////////////////////////// BEGINNING ////////////////////////////////////////////////////////
cout << endl << "Admiral " << name << ", we need you on flight deck.";
cout << "1: Go to flight deck.";
cout << "2: Go to kitchen.";
cout << "3: Go to MedBay.";
cout << "4: Do nothing.";
}
and I get an error for:
cin >> name;
where ">>" matches no operands.
I clearly remember this code working at some point I do believe. If I try and skip ahead, I get an error where it can't find an exe (and there is no option to Build Final)
Sorry for not being clear, but I haven't used C++ for a few years now, quite rusty on just about everything. Any sort of wisdom to shed?
You need to #include <string>. That's where the actual operator is defined. Chances are that in the past, <iostream> may have included it perchance, which it's allowed to but not required (or guaranteed) to.