print out string in a string

HI!
I have some lines of code that need your help.
When I run this program, I'd like to type in a string named "Resist" at line number 18, and print out a component string, it's R.Rvalue like at line number 23 and 24. for instance, if I type: R3{R 4.5}, expected result is string "4.5", but this program prints out nothing.
Can anyone please explane it and way to fix?
thanks for your help
Here is my code:

#include <iostream>
#include <stdio.h>
#include <string>
#include <stdlib.h>

using namespace std;

struct Resistor
{
int i;
string Rvalue;

};

int main()
{
struct Resistor R;
string Resist = "R" + to_string(R.i) + "{R" + " " + (R.Rvalue) + "}";

cout << "Enter Resistor: " << endl;
getline(cin, Resist);
cout << Resist << endl;
string s = R.Rvalue;
cout << "value=" << s<<endl;

//printf("value=%f", R.N Rvalue);
system("pause");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
#include <regex>
#include <iomanip>

// returns a negative value for badly formed input
double extract_number( const std::string& input )
{
    // http://en.cppreference.com/w/cpp/regex
    // for an explanation of this regular expression, see:
    // http://xenon.stanford.edu/~xusch/regexp/analyzer.html?regex=%5E%5Cs*R%5Cd+%5Cs*%5C%7B%5Cs*R%5Cs+%28%5Cd+%28%3F%3A%5C.%5Cd*%29%3F%29+%5Cs*%7D%5Cs*%24&env=env_java
    static const std::regex re( R"(^\s*R\d+\s*\{\s*R\s+(\d+(?:\.\d*)?)+\s*}\s*$)" ) ;

    std::smatch match ;
    if( std::regex_match( input, match, re ) ) return std::stod( match[1] ) ;
    else return -100 ;
}

int main()
{
    for( std::string str : { "R3{R 4.5}", " R32 { R 4.567 } ", "R3{R 41}", "R3{R 54.}", "R3{R }4.5}" } )
    {
        std::cout << std::quoted(str) << "  " << std::fixed ;

        const double value = extract_number(str) ;
        if( value < 0 ) std::cout << "badly formed input\n" ;
        else  std::cout << "value: " << value << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/ef978961763ad64c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <stdio.h>
#include <string>
#include <stdlib.h>

using namespace std;

struct Resistor
{
int i;
string Rvalue;

};

int main()
{
struct Resistor R;
string Resist = "R" + to_string(R.i) + "{R" + " " + (R.Rvalue) + "}";

cout << "Enter Resistor: " << endl;
getline(cin, Resist);
cout << Resist << endl;
string s = R.Rvalue;
cout << "value=" << s<<endl;

//printf("value=%f", R.N Rvalue);
system("pause");
}


The problem with your code is that at line 17 you define a variable R with an empty RValue and some random value for i.
On line 23 you assign this empty string R.RValue to a string s and wonder why s is empty.
Why do do assign a value to Resist on line 18 when you override it at line 20 ?
i and Rvalue, which I'd like to enter it when i run this program
Why is i an int when you want to read a double or float value ?

The best way is the solution JLBorges posted but probably to difficult to understand.

It seems this exercise is above your current skill level. I would suggest that you first study the basics of input, string and stringstream class and postpone this exercise.

The only quick and dirty solution would be sth. like this. If you can live with it....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int main()
{
  const string input = "R3{R 4.5}";
  char dummy[5] = {0};
  double val = 0;

  if (sscanf(input.c_str(), "%4s %lf}", dummy, &val))
    cout << "Val = " << val << "\n";
  else
    cout << "Error " << "\n\n";
}

Thanks alot!
Topic archived. No new replies allowed.