I have an output file name out
using the below code to add a string to the text file:
string foo = "Hello, foo";
out << foo;
How can I customize a string to input into out file
adding string and numbers with a specific width using setw(7)
Your name is:AName you are 18
Your name is:foo you are 30
with variable name holding the name and variable age holding the age
how can I make this code works
out<< ("Your name is :"+ setw(7)+ name +" you are " + age);
You could try something like this:
std::string name = "AName";
unsigned int age = 18;
out << "Your name is:" << setw(7) << name << "you are " << age << "\n";
If you have a struct and database, this might be:
struct Name_Age
{
std::string name;
unsigned int age;
};
int main()
{
std::vector<Name_Age> database;
Name_Age record;
record.name = "AName"; record.age = 18;
database.push_back(record);
record.name = "foo"; record.age = 30;
database.push_back(record);
for (size_t index = 0; index < database.size(); ++index)
{
cout << "Your name is:" << setw(7) << database[index].name
<< "you are " << database[index].age << "\n";
}
return 0;
}
It is just as simple as
std::out << "Your name is :" << std::setw(7) << std::left << name << " you are " << age;
setw does not return a string that you can concatenate. It returns an unspecified type that can be passed to operator << of an output stream.
Related
if(gamble == "You Get A Raise"){
int raise[] {1,2,3,4,5,6,7,8,9,10}
cout << "You get a " raise[rand()%10] << " $ Raise" << endl;
salary = salary + raise;
}
Im trying to make a program that picks out a number 1-10 then adds that to your current salary
You actually to store the index of the raise: rand()%10 in order to display and then apply the same raise to the salary.
int raise[] {1,2,3,4,5,6,7,8,9,10};
int raiseIdx = rand()%10;
cout << "You get a " << raise[raiseIdx] << " $ Raise" << endl;
salary = salary + raise[raiseIdx];
Your gamble variable should be a std::string:
std::string gamble = "You Get A Raise";
You forgot a <<?
Try this:
int raise[] = {1,2,3,4,5,6,7,8,9,10};
int raise_value = rand()%10;
cout << "You get a " << raise[raise_value] << " $ Raise" << endl;
salary += raise[raise_value];
Your string gamble needs to be std::string too:
std::string gamble = "You Get A Raise";
Here is a full example:
int salary = 1000;
std::string gamble = "You Get A Raise";
int raise[] = {1,2,3,4,5,6,7,8,9,10};
if (std::strcmp(gamble.c_str(),"You Get A Raise") == 0) {
// or
// if (gamble.compare("You Get A Raise") == 0) {
int raise_value = rand()%10;
cout << "You get a " << raise[raise_value] << " $ Raise" << endl;
salary += raise[raise_value];
cout << "New salary is $" << salary << endl;
}
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
// declares variables
string dayow;
string month;
string day;
string year;
int main()
{
cout << "Pick you day of the week (ex: Monday-Sunday)" << endl;
getline(cin, dayow);
cout << " " << endl;
cout << "Pick your month (ex: January-December)" << endl;
getline(cin, month);
cout << " " << endl;
cout << "Pick your day of the month (ex: 1-31)" << endl;
getline(cin, day);
cout << " " << endl;
cout << "Pick your year" << endl;
getline(cin, year);
cout << " " << endl;
cout << "This is your date.." << endl;
cout << dayow << ", " << month << " " << day << ", " << year << "." << endl;
cout << " " << endl;
cout << "Here are the 3 formats to display your date.." << endl;
cout << " " << endl;
cout << "1. " << month << " " << day << " was a " << dayow << " in " << year << endl;
cout << " " << endl;
std::string str = dayow;
std::string str1 = str.substr(0, 3);
std::string str = month;
std::string str2 = str.substr(0, 2);
std::string str = day;
std::string str3 = str.substr(0, 2);
std::string str = year;
std::string str4 = str.substr(0, 4);
std::cout << str1 << ", " << str2 << " " << str3 << " '" << str4 << endl;
return 0;
}
So what i'm trying to do here is get the user to input ex: Tuesday, March 22, 2012, then have 2 results come out. 1st being "January 1 was a Tuesday in 2012", that is fine as it is. Where the problem lies is the second result where I want "Tue, Mar 22, 2012" but the problem is on line 46 - 53, all of the strings are connecting to the first std:string str = dayow; so the output turns into TueTuTuTues!
Can anyone help?? thanks!
EDIT : Sorry if this is a noob questions :/ really new to coding!
Try this one: (some changes of your code)
#include <iomanip>
#include<iostream> // **add this header file**
#include <string>
#include <fstream>
using namespace std;
// declares variables
string dayow;
string month;
string day;
string year;
int main()
{
cout << "Pick you day of the week (ex: Monday-Sunday)" << endl;
getline(cin, dayow);
cout << " " << endl;
cout << "Pick your month (ex: January-December)" << endl;
getline(cin, month);
cout << " " << endl;
cout << "Pick your day of the month (ex: 1-31)" << endl;
getline(cin, day);
cout << " " << endl;
cout << "Pick your year" << endl;
getline(cin, year);
cout << " " << endl;
cout << "This is your date.." << endl;
cout << dayow << ", " << month << " " << day << ", " << year << "." << endl;
cout << " " << endl;
cout << "Here are the 3 formats to display your date.." << endl;
cout << " " << endl;
cout << "1. " << month << " " << day << " was a " << dayow << " in " << year << endl;
cout << " " << endl;
// **changes start here**
string str1 = dayow.substr(0, 3);
string str2 = month.substr(0, 3);
string str3 = day.substr(0, 2);
string str4 = year.substr(0, 4);
cout << str1 << ", " << str2 << " " << str3 << " ," << str4 << endl;
return 0;
}
Which compiler do you use? Your program shouldn't compile.
Add #include <iostream>
You cann't define few variables with same name C++.
So replace
std::string str = dayow;
std::string str1 = str.substr(0, 3);
std::string str = month;
std::string str2 = str.substr(0, 2);
std::string str = day;
std::string str3 = str.substr(0, 2);
std::string str = year;
std::string str4 = str.substr(0, 4);
with
std::string str1 = dayow.substr(0, 3);
std::string str2 = month.substr(0, 2);
std::string str3 = day.substr(0, 2);
std::string str4 = year.substr(0, 4);
change this part:
std::string str = dayow;
std::string str1 = str.substr(0, 3);
std::string str = month;
std::string str2 = str.substr(0, 2);
std::string str = day;
std::string str3 = str.substr(0, 2);
std::string str = year;
std::string str4 = str.substr(0, 4);
to:
std::string str = dayow;
std::string str1 = str.substr(0, 3);
std::string stra = month;
std::string str2 = stra.substr(0, 3);
std::string strb = day;
std::string str3 = strb.substr(0, 2);
std::string strc = year;
std::string str4 = strc.substr(0, 4);
problem was you had same variable name for day,month and year
I keep getting this error,
error: ‘std::string’ has no member named ‘get_name’
cout << name.get_name() << name.get_won() << name.get_lost << endl << endl;
even though 'name = name_of_player;' and 'cout << name;' prints the correct name. I am guessing that it is taking the variable and not the actual string? I am not entirely sure, thus I need help.
#include <iostream>
#include <string>
using namespace std;
class person {
string name_;
int won_;
int lost_;
public:
void set_name(string);
void set_wl(int,int);
int get_won() const {return won_;}
int get_lost() const { return lost_;}
string get_name() const {return name_;}
};
void person :: set_name(string n) {
name_ = n;
}
void person :: set_wl(int x, int y) {
won_ = x; lost_ = y;
}
int number_of_players;
string name;
string name_of_player[31];
int counter;
void get_player()
{
counter = 1; //Initiating the counter
cout << endl << "Ok! Now we are going to enter their names in. " << endl;
for (int i=1; number_of_players >= i; i++)
{
cout << "Enter player #" << counter << "'s name... " << endl;
cout << "The max is 30 players... " << endl;
getline (cin, name);
cout << endl;
cout << "Okay, so player #" << counter << " is " << name << "." << endl;
cout << "Please press Enter to continue..." << endl;
cin.ignore();
name_of_player[counter] = name;
person name;
name.set_name(name_of_player[counter]);
name.set_wl(0,0);
counter = (counter + 1);
}
}
void list_players()
{
counter = 1; //initiating the counter
cout << "Here are the current players with their respective win/loss ration... " << endl << endl;
for (int i=1; number_of_players >= i; i++)
{
name = name_of_player[counter];
cout << name.get_name() << name.get_won() << name.get_lost << endl << endl;
counter = counter +1;
}
}
int main()
{
cout << "Welcome to the Chess Tournament Organizer. " << endl;
cout << "How many people will be playing today? " << endl;
cin >> number_of_players;
cin.ignore();
get_player();
list_players();
return (0);
}
******************************************************************************
EDIT: I have solved it and figured it out, past all of my terrible code. The problem was, for every name, I was creating a class of 'person' with that name. But this was bad because it was a local scope. So what I did was change the global string 'name_of_players' to a global variable of the person class with
person name_of_player[31];
Changing also...
cout << "Enter player #" << counter << "'s name... " << endl;
cout << "The max is 30 players... " << endl;
getline (cin, name);
cout << endl;
cout << "Okay, so player #" << counter << " is " << name << "." << endl;
cout << "Please press Enter to continue..." << endl;
cin.ignore();
name_of_player[counter].set_name(name);
name_of_player[counter].set_wl(0,0);
And doing the same to the 'list_of_players' function.
I am still not completely sure why it didn't work, I wanted several 'person's identified by their names, created by using the name given. So if a user were to input "mike" there would be a person class called mike, and I could find the information of the person class mike, by inputing mike.get_info();
TL;DR
I needed to fix the scope and not run my string as a name of the person class for each person.
name_of_player is defined as an array of 31 strings:
string name_of_player[31];
You get the element from that array enumerated by counter when you do:
name = name_of_player[counter];
So name is a string. strings do not have a get_name method.
You may be confused about what name is cause you define a local person name in get_player, but then you never use that locally defined name.
An ugly way to solve this would be to create another global where you declare name_of_player as:
person stats_of_player[31];
Then at the bottom of the for-loop in get_player you can assign the locally declared variable to the global array:
stats_of_player[counter] = name;
You would then be able to use stats_of_player in list_players instead of your for-loop as follows:
for (int i=1; number_of_players >= i; i++)
{
cout << stats_of_player[i].get_name() << stats_of_player[i].get_won() << stats_of_player[i].get_lost() << endl << endl;
}
name_of_player is array of strings, not person objects. So you trying to call get_name on string object and this is why you are getting such error.
Running your code I get that std::string has no member named get_name, get_won, or get_lost. This is because you are trying to invoke those methods on a c++ string object, which does not have those methods.
Furthermore it seems like you expect name_of_player to be an array of players when it is in fact an array of strings.
Because the methods of your person class are public and not static you need an instance of your person class to invoke those methods (outside of the class definition e.g. main).
void get_player() {
....
player p();
p.set_name("Bob");
p.get_name();
...
}
I want to replace a set of strings between << >> delimiters.
For example say
int age= 25;
string name= "MYNAME";
string test = My age is << your age >> and my name is << your name >>.
Output should be
My age is 25 and my name is MYNAME.
What is the best method to do this is c++ ?
try this
#include <iostream>
#include <string>
int main ()
{
std::string str ("My age is << your age >> and my name is << your name >>.");
std::string str2 ("<< your age >>");
std::string str3 ("<< your name >>");
str.replace(str.find(str2),str2.length()," 22 ");
str.replace(str.find(str3),str3.length()," Nooh ");
std::cout << str << '\n';
return 0;
}
Im not sure I understand the question, but if its what I think it is, try
string test = "My age is " + age + " and my name is " + name + ".";
If you are bent on using << and >>, you can do
cout << "My age is " << age << " and my name is " << name << "." << endl;
I can not seem to figure out why the get_password function call will always return qwert no matter what string I pass into the function. My problem is that I can not see what is going wrong with the string comparison in this function.
string get(string askfor, int numchars, string input)
{
cout << askfor << "(" << numchars << " characters): ";
cin >> input;
return input;
}
string get_password(string name)
{
string pwd;
if (name == "botting"){
pwd = "123456";
}
else if (name == "ernesto") {
pwd = "765432";
}
else if (name == "tong") {
pwd = "234567";
}
else {
pwd = "qwert";
}
return pwd;
}
int main()
{
string name;
string pwd;
string passwd;
cout << "Address of name =" << &name << "\n";
cout << "Address of pwd =" << &pwd << "\n";
cout << "Address of passwd =" << &passwd << "\n";
bool authenticated = false;
while (!authenticated)
{
// call one
string name1 = get("Name", 7, name);
cout << "call one returned: " << name1 << endl;
// call two
string pass1 = get_password(name);
cout << "call two returned: " << pass1 << endl;
//call three
string pass2 = get("Password", 7, passwd);
cout << "call three returned: " << pass2 << endl;
// compare the two passwords
authenticated = false;
if (pass1 == pass2) {
cout << "Welcome " << name << "\n";
authenticated = true;
}
else {
cout << "Please try again\n";
}
}
return 0;
}
pass name1 to the second call:
// call one
string name1 = get("Name", 7, name);
cout << "call one returned: " << name1 << endl;
// call two
string pass1 = get_password(name1); // change this variable
cout << "call two returned: " << pass1 << endl;
the get() returns the name to name1 string and does not update name variable itself, because of this name remains empty string.
That's because you never assign name anything. It is initialized as "", and that never changes. Since that matches none of the cases in get_password, it always falls into the else case, which yields "qwert".
I think the issue is that your get function would be better written like:
string get(string askfor, int numchars)
{
string input; // input shouldn't be an argument
cout << askfor<<"("<<numchars<<" characters): ";
cin >> input;
return input;
}
And you can use the result of that to assign into name:
name = get("Name", 7);
You just need to pass by reference rather than value.
string get(string askfor, int numchars, string &input)
{
cout << askfor << "(" << numchars << " characters): ";
cin >> input;
return input;
}