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;
Related
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.
This question already has answers here:
Removing trailing newline character from fgets() input
(14 answers)
Closed 5 years ago.
#include<iostream>
using namespace std;
int main()
{
char str[10] = "Anmol" ;
int age = 17 ;
cout << "Enter your name here :- " ;
fgets(str, sizeof(str), stdin) ;
cout << "Enter your age here :- " ;
cin >> age ;
cout << "Hello World, It's " << str << "And my age is " << age ;
return 0 ;
}
On running the code, the compiler is giving output in different line like:-
fgets() is a file function which is used to read text from keyboard, as in “file get string.”
fgets() function is read the string as well as "enter" character ascii code which is 13 (carriage return - CR) .so the above code consider the CR character at the end of the 'str' that's why it print in the next line .
You can use the gets_s() function to take the string from the keyboard.
Try the below code .
#include<iostream>
using namespace std;
int main()
{
char str[10] = "Anmol";
int age = 17;
cout << "Enter your name here :- ";
gets_s(str);
cout << "Enter your age here :- ";
cin >> age;
cout << "Hello World, It's " << str << " And my age is " << age;
return 0;
}
try replace '\r\n', and '\n\r' with '' in str
look at here for replace in string : How to replace all occurrences of a character in string?
Try this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
int age;
cout << "Enter your name here :- " ;
cin >> str;
cout << "Enter your age here :- " ;
cin >> age ;
cout << "Hello World, It's " << str
<< " And my age is " << age << endl;
return 0 ;
}
When you use fgets(), you also get the ending newline character in the input. That explains your output. You could use std::getline to avoid that problem.
int main()
{
std::string str = "Anmol" ;
int age = 17 ;
cout << "Enter your name here :- " ;
std::getline((std::cin, str) ;
cout << "Enter your age here :- " ;
cin >> age ;
cout << "Hello World, It's " << str << " and my age is " << age << std::endl;
return 0 ;
}
#include <iostream>
using std::cout;
using std::cin;
using std::string;
int main(){
cout << "Welcome to the program!";
cout << "\nWhat is your name? ";
string name;
cin >> name;
cout << "Hi, " << name << ". ";
cout << "Your name has " << name.length() << " letters!";
cout << "\nWhat is your last name? ";
string lastname;
string *plastname;
cin >> lastname;
plastname = &lastname;
cout << "Your full name is " << name.append(*plastname) << ".";
cout << " Your full name has " << name.length() + lastname.length() << " letters!";
return 0;
}
And this is the results:
Welcome to the program!
What is your name? adk
Hi, adk. Your name has 3 letters!
What is your last name? adkl
Your full name is adkadkl. Your full name has 11 letters!
How 3 + 4 = 11?!
First you do name.append(*plastname) which really appends lastname into name, making name a string of length 7 (with your example input).
Then you print name.length() + lastname.length() which is equal to 7 + 4 (remember the previous append you did!), leading to the result of 11.
Perhaps of appending name and lastname, you should print them separately? Like
cout << "Your full name is " << name << ' ' << lastname << ".\n";
You've appended lastname to name and then added them, that gives you a bigger value than expected
cout << "Your full name is " << name.append(*plastname) << ".";//You append here
cout << " Your full name has " << name.length() + lastname.length() << " letters!";//And then add here
you just print name.length() instaed of name.length() + lastname.lenght() as you already appended the lastname to name.
I am new to c++ but do have a basic knowledge in coding. This program works fine and well but I'm wondering if there is a better way to do this.
The program makes a star wars name by taking the first three letters of your last name and the first 2 of your first name to make your first name of your star wars name. Then for your star wars surname it takes the first two letters of your mother's maiden name and the first three letters of the city you were born in.
// starWarsName.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string firstName;
string surname;
string maidenName;
string city;
cout << "This program is designed to make you a star wars name, it takes some information and concatinates parts of the information to make your NEW name" <<endl << endl;
cout << "please enter your first name" << endl;
cin >> firstName;
cout << "please enter your surname" <<endl;
cin >> surname;
cout << "what is your mothers maiden name?" << endl;
cin >> maidenName;
cout << "please tel me which city you were born in" << endl;
cin >> city;
cout << firstName << " " << surname << endl;
cout << firstName[0] << " " << surname << endl;
int size = firstName.length();
//cout << size;
cout << surname[0] << surname[1] << surname[2] << firstName[0] << firstName[1];
cout << " " << maidenName[0] << maidenName[1] << city[0] << city[1] << city[2];
cin.get();
cin.ignore();
return 0;
}
You can use string::substr here to store character sequence instead of writing surname[0]..surname[2] again and again.
Here is an example of string::substr
#include <iostream>
#include <string>
int main ()
{
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (3,5); // "think"
std::size_t pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos); // get from "live" to the end
std::cout << str2 << ' ' << str3 << '\n';
return 0;
}
Output:
think live in details.
#include <iostream>
#include <string>
using namespace std;
int main() {
double age;
double months;
string name;
months = age*12.0;
cout << "Enter your name and age: ";
cin >> name >> age;
cout << "Hello " << name << " age " << age << " (" << months << " months)\n";
return(0);
}
The program asks for name and age, and should out put the name and age in a sentence with the age in months in parentheses.
Output gives something like:
Hello Bob age 20 (1.82561e-313 months), but should be Hello Bob age 20 (240 months). I did not use int because I wanted to be able to input non int values for age.
I have tried 12 instead of 12.0 and tried declaring a variable and doing months = age*m where m = 12.0 but is result is the same. By the way, the random value is about the same regardless of what variable age is. Why is this happening? Also, would this be a link-time error or run-time error?
You're doing the multiplication using age before it's initialized. What value would you expect it to give you?
Change your code to first get the age, and then do the multiplication:
cout << "Enter your name and age: ";
cin >> name >> age;
months = age*12.0;
cout << "Hello " << name << " age " << age << " (" << months << " months)\n";
Move the calculation of months to after the point age is entered (age was uninitialised in the original code):
cout << "Enter your name and age: ";
cin >> name >> age;
months = age*12.0;
cout << "Hello " << name << " age " << age << " (" << months << " months)\n";