Printing a variable to change it !

is there any way to print a variable, and edit it on line ????

ex:
1
2
3
4
5
line1.   string name1="Marc"; string name2;
line2.   cout<<"Your name is :"<<name1;
line3.   getline(cin, name2);
line4.   if(!name2.empty())
line5.        name1 = name2;

this code does display the name1, but does not allow me to edit it !
I want to know if there is any possibility in c++ to print the value of "name"
and change it if I want.
Last edited on
Yes, actually, via the GNU Readline library.
https://cnswww.cns.cwru.edu/php/chet/readline/rltop.html

But that is a probably a bit of an overkill. Why not just ask anew?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

string validate_name( const string& name )
{
  cout << "Your name is " << name << ".\n"
          "If correct, press Enter. Otherwise type the correct name: ";
  string new_name;
  getline( cin, new_name );
  return new_name.empty() ? name : new_name;
}

int main()
{
  string user_name = validate_name( "Marc" );
  cout << "Nice to meet you, " << user_name << "!\n";
}

Hope this helps.
Thank you,
How can I print it using te ostream operator that exist in an other file ?
Ex:
Client.cpp contain my ostream function
Client.h contain the line that makes it a friend
And main.cpp contain The function that print the name with with the ostream "-"
The insertion operator (ostream operator) is for printing your own structures. Example of multiple files:

point.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef POINT_HPP
#define POINT_HPP

#include <iostream>

struct point
{
  double x, y;
  point(): x(), y() { }
  point( double x, double y ): x(x), y(y) { }
};

std::ostream& operator << ( std::ostream&, const point& );

#endif 

point.cpp
1
2
3
4
5
6
#include "point.hpp"

std::ostream& operator << ( std::ostream& outs, const point& p )
{
  return outs << "(" << p.x << "," << p.y << ")";
}

main.cpp
1
2
3
4
5
6
7
8
9
#include <iostream>
#include "point.hpp"

int main()
{
  point p( -7, 4.3 );

  std::cout << "I have a point at " << p << ".\n";
}

Compile and link your .cpp files together.
With GCC: g++ -Wall -pedantic -std=c++14 -O2 -s main.cpp point.cpp
With MSVC: cl /EHsc /O2 main.cpp point.cpp
Etc.

Good luck!
oh, looks so easy now!
i got it, thank you for your help !
Topic archived. No new replies allowed.