Cygwin "using: command not found" for C++ program - c++

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!

Related

Fix scripts Disappear when use getline() Function in C++

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

compiling is okay but my program doesn't really work

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
while (1)
{
char name1[100];
char adrs1[100];
char rsn1[100];
char XXXXX[100];
cout << "input personal information" << '\n';
cout << "patient 1" << '\n';
cout << "input the name of the patient" << '\n';
cin.getline (name1,100);
cout << "input the address of the patient" << '\n';
cin.getline (adrs1,100);
cout << "input the reason" << '\n';
cin.getline (rsn1,100);
cout << "input the name of the patient" << '\n';
cout << "if you want to exit, input exit" << '\n';
cin.getline (XXXXX,100);
if (XXXXX==name1)
cout << adrs1[100] << rsn1[100] << '\n';
else (XXXXX=="exit");
break;
return 0;
}
}
that's my program, and compiling is okay. but when i start the program, it doesn't print any rsn or adrs, it just ends.
I want it to print rsn and adrs when it reads names.
Help me please
There are quite a few errors in your program.
The most important one is that you are trying to write an infinite loop. But it runs exactly once. You need to move your return statement out of the loop.
There is no need for a conditional statement for an else block. You can remove it along with the semi colon.
You're trying to print a character at the index 100 which goes out of bounds.
I don't know what XXXXX is supposed to be. May be you missed pasting the declaration on this website.
At this point, I really suggest picking up a book or trying to debug your code by going step-by-step through your code. It would be more helpful to you at this stage in your learning than this website,
To complete the answer above, the correct program would be:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
while (1)
{
char name1[100];
char adrs1[100];
char rsn1[100];
char XXXXX[100];
cout << "input personal information" << '\n';
cout << "patient 1" << '\n';
cout << "input the name of the patient" << '\n';
cin.getline (name1,100);
cout << "input the address of the patient" << '\n';
cin.getline (adrs1,100);
cout << "input the reason" << '\n';
cin.getline (rsn1,100);
cout << "input the name of the patient" << '\n';
cout << "if you want to exit, input exit" << '\n';
cin.getline (XXXXX,100);
if (strcmp(XXXXX,name1) == 0)
cout << adrs1 << rsn1 << '\n';
else /*(XXXXX=="exit");*/
break;
//return 0;
}
}
You forgot to initilize the name1 variable, you can initialize it using char name1[100] = {};
You cannot directly compare the if (XXXXX==name1), use can use the strncmp function for the same. I will prefer the string class instead of char pointer. Use the following:
if (!strncmp(XXXXX,name1,100))
cout << adrs1 << rsn1 << '\n';
else if (!strncmp(XXXXX,"exit",100))
break;

String phrase ends at space?

I am wringing a simple code to learn more about string. When I ran my code it would not print my last name. Can someone explain why? I used string phrase to store it and it only appears to have stored my first name. Here is the code.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
cout << "Exercise 3B" << endl;
cout << "Kaitlin Stevers" << endl;
cout << "String arrays" << endl;
cout << endl;
cout << endl;
char greeting[26];
cout << "Please enter a greeting: " << endl;
cin >> greeting;
cout << "The greeting you entered was: " << greeting << endl;
string phrase;
cout << "Enter your full name " << endl;
cin >> phrase;
cout << greeting << ", how are you today " << phrase << "?" << endl;
return 0;
}
I used string phrase to store it and it only appears to have stored my first name.
That makes sense.
cin >> phrase;
will stop reading when it encounters a whitespace character in the input.
To read the full name you can use one of the following approaches.
Use two calls to cin >>.
std::string first_name;
std::string last_name;
cin >> first_name >> last_name;
Use getline to read the entire line. getline will read everything in a a line, including whitespace characters.
getline(cin, phrase);
When you call cin >> phrase;, it only reads the string up to the first non-space character. If you want to include spaces in your name, best goes with getline(cin,phrase);.
IMPORTANT: getline() will reads whatever it is in the stream buffer up to the first \n. It means that when you enter cin >> greeting;, if you hit ENTER, getline() will read everything before that \n that is not already read, which is NOTHING into your phrase variable, making it an empty string. An easy way out is to call getline() twice. E.g.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
cout << "Exercise 3B" << endl;
cout << "Kaitlin Stevers" << endl;
cout << "String arrays" << endl;
cout << endl;
cout << endl;
char greeting[26];
cout << "Please enter a greeting: " << endl;
cin >> greeting; //IMPORTANT: THIS ASSUME THAT GREETING IS A SINGLE WORD (NO SPACES)
cout << "The greeting you entered was: " << greeting << endl;
string phrase;
cout << "Enter your full name " << endl;
string rubbish_to_be_ignored;
getline(cin,rubbish_to_be_ignored); //this is going to read nothing
getline(cin, phrase); // read the actual name (first name and all)
cout << greeting << ", how are you today " << phrase << "?" << endl;
return 0;
}
Assuming you store that code in the file stackoverflow.cpp. Sample run:
Chip Chip#04:26:00:~ >>> g++ stackoverflow.cpp -o a.out
Chip Chip#04:26:33:~ >>> ./a.out
Exercise 3B
Kaitlin Stevers
String arrays
Please enter a greeting:
Hello
The greeting you entered was: Hello
Enter your full name
Kaitlin Stevers
Hello, how are you today Kaitlin Stevers?
Tested on ubuntu 14.04

Code that once worked, no longer works? Visual C++ 2010

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.

Compiling my scripting language

I have developed a frame work for a scripting language and I am trying to make a compiler for it to output onto a certain file a custom Hex code to be read by an interpreter for applications such as games and movies. The interpreter will read the hex and perform operations via loops and pretty much do the hard work for me. But I need a way to compile the strings of text into my custom hexadecimal. Here is an example of my compiler.
#include <iostream>
#include <string>
#include <Windows.h>
#include <wincon.h>
#include <fstream>;
using namespace std;
int main(){
char egScriptpath[999];
char path[999];
char title[999];
ifstream readfile;
ofstream myfile;
int optn;
cout << "Enter the path where your *.egSCRIPT file is: " << endl;
cin.getline(egScriptpath,999);
cout << "Enter your path where you want to compile your *.egSCRIPT file: " << endl;
cin.getline(path,999);
cout << "Enter your title for your program: ";
cin.getline(title,999);
cout << endl << "Enter for your option of file extention:" << endl;
cout << " Enter 1 for .egGame :" << endl;
cout << " Enter 2 for .egMovie: ";
cin >> optn;
if(optn==1){
readfile.open((string)egScriptpath);
ofstream egMovieFile((string)path+"\\"+(string)title+".egGame");
myfile.open((string)path+"\\"+(string)title+".egGame");
while(!readfile.eof()){
if(readfile.getline("validated()",1)){
myfile << " \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"
" \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"
" \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"
" ////////////////\n"
" ////////////////\n"
" ||||||||||||||||\n"
" ||||||||||||||||\n"
" ||||||||||||||||\n"
" \1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\n"
" \2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\n";
}
}
myfile.close();
readfile.close();
}else if(optn==2){
readfile.open((string)egScriptpath);
ofstream egGameFile((string)path+"\\"+(string)title+".egMovie");
myfile.open((string)path+"\\"+(string)title+".egMovie");
while(!readfile.eof()){
if(readfile.getline("validated()",1)){
myfile << " \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"
" \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"
" \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"
" ////////////////\n"
" ////////////////\n"
" ||||||||||||||||\n"
" ||||||||||||||||\n"
" ||||||||||||||||\n"
" \1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\n"
" \2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\n";
}
}
myfile.close();
readfile.close();
}
cout << "Compile complete" << endl;
system("pause");
return 0;
}
So what I want to do is in the if statement where it says if(readfile.getline("validated()",1)){} I want it to write onto the "myfile" ofstream variable the text that you can see just for an example. I hope I am clear on what I am saying. If the compiler reads "validated()" on my script file, then it will write the "hexadecimal" code onto the new file with the variable of "myfile" which will in return be interpreted by the interpreter for applications such as games and movies. Thanks if you know what is wrong and why it is not writing the hex code onto the new file that it makes.
Looks like your problem is located here:
if(readfile.getline("validated()",1)){
Sincerely, I don't even know why the compiller allows you to do so, but I would like to suggest to you the following change:
instead of:
while(!readfile.eof()){
if(readfile.getline("validated()",1)){
use this one:
std::string buffer;
while (std::getline(readfile, buffer, '\n')){
if (buffer == "validated()")
also, since you are using C++, instead of use arrays of char, you should relly more on std::string class.