I'm having difficulty working with C++'s strings. I'm writing a program to solve a set of linear equations. I'd like to save the solution for various values of N (the size of the system is N X N), and n, a parameter within the system, to a text file, for later analysis. Ideally, I'd like to create a program that lets me enter a value for N (eg. 50 or 4000) and n (eg. -3 or -5) and then saves the solution to the system to a file called "50N_-3n.dat" or something.

1. How do I enter an int, and then make it a string? the following, stolen from the internet, does not do it:
string int_to_s(int num, string& converter){
toString << num;
converter = toString.str();
return converter;
}

This returns: myfirst.cpp: In function ‘int main()’:
myfirst.cpp:12: error: a function-definition is not allowed here before ‘{’ token

I've placed this function definition well below int main() {

2. Once I create a string with value "50", how do I glue it into an output file name?

string name;
name="myhouse_" en ".dat";
ofstream outfile;
outfile.open(name);

In this test code, en is supposed to be "50", and the file should be myhouse_50.dat. C++ says:
myfirst.cpp:28: error: expected `;' before ‘en’
myfirst.cpp:29: error: aggregate ‘std::ofstream outfile’ has incomplete type and cannot be defined

How do you get outfile.open to understand the VALUE of name?
As you can probably tell, I have very little experience with C++. Thanks,

Dave

Recommended Answers

All 3 Replies

For appending a string with another, use the append() from the string type. Use this link for more info:

http://www.cplusplus.com/reference/string/string/append.html

For converting a string to an integer, use atoi() function from the C library.
For converting an integer to string, use itoa() function from the C library.

That sounds like exactly what I need, but it doesn't seem to function correctly. I copied the itoa code directly from the cplusplus.com web page:

/* itoa example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int i;
  char buffer [33];
  printf ("Enter a number: ");
  scanf ("%d",&i);
  itoa (i,buffer,10);
  printf ("decimal: %s\n",buffer);
  itoa (i,buffer,16);
  printf ("hexadecimal: %s\n",buffer);
  itoa (i,buffer,2);
  printf ("binary: %s\n",buffer);
  return 0;
}

C++ complains:
scratch.cpp: In function ‘int main()’:
scratch.cpp:11: error: ‘itoa’ was not declared in this scope

As I said, I'm new to C++. I thought that's what those include statements do.

1. How do I enter an int, and then make it a string? the following, stolen from the internet, does not do it:
string int_to_s(int num, string& converter){
toString << num;
converter = toString.str();
return converter;
}

There are multiple ways to convert from an int to a string. One was is to use itoa() function to do so but it will store it in a char array first, which is easily fixed. However, it doesn't calculate for buffer overflow, so it can crash the system.

#include <iostream>
#include <string>
#include <stdio.h>	
using namespace std;

int main() {
	int num;
	char array[5];
	
	cout << "Enter a number that is less then 5 characters long.\n";
    cin >> num;
	// convert 123 to string [buf]
	itoa(num, array, 10);

	// print our string
	printf("In array:  %s\n", array);
	
	string s (array);
	cout << "In string: " << s << endl;
	
	return 0;
}

Another sort of unconventional way is to use stringstream to do it. It takes the number and throws it into a buffer, which you can then tell it to store in a string. I've been told that this way isn't that great, don't remember why, but it does work.

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main() {
	int num;
    stringstream buf;
    string s;
	
	cout << "Enter a number: ";
    cin >> num;

    buf << num;
    buf >> s;
    buf.clear();
    
	cout << "In string: " << s << endl;

	return 0;
}

2. Once I create a string with value "50", how do I glue it into an output file name?

Below is how I did it. When using outfile.open(name) where name is a string, you need the name.c_str() or it will not like it (sorry, I can't remember exact reason, it's been a while since I've used that). And when trying to set name equal to other strings, us + to show that they are added together to form the string that will be declared as name. Everything is below.

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;

int main() {
    string s = "50", name;
    ofstream dout;
	
    // Saves the file name in name
    name = "myhouse_" + s + ".dat";
    
    // Shows what the name consists of
    cout << name << endl;
	
    // Opens and stores in document
    dout.open(name.c_str());

    // Below should appear in myhouse_50.dat
    dout << "HI.  I'm in the file.";	

    return 0;
}

If that isn't clear, feel free to ask. I can explain my madness.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.