Assigning variables to strings

This is a discussion on Assigning variables to strings within the C++ Programming forums, part of the General Programming Boards category; Hi, I have created a program that takes a users input, runs it through a if, else if, else, and ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    118

    Question Assigning variables to strings

    Hi,

    I have created a program that takes a users input, runs it through a if, else if, else, and prints a result on the screen. I have also set it up so that it creates a text file (which I am hoping to display what I have it printed out on the screen). Heres what I have:

    Code:
    #include <iostream>
    #include <fstream>
    
    using namspace std;
    
    int main()
    {
    int number;
    char numberwithspelling[50]
    
    cout<<"Type A Number Between 1 and 20: ";
    cin>>number;
    cin.ignore();
    
    if(number==0)
    {
    cout<<"Please enter a valid number";
    }
    .....
    if(number==20)
    {
    cout<<"You Entered The Number Twenty";
    }
    
    else
    {
    cout<<"Please enter a valid number";
    }
    cin.get();
    
        std::ofstream outfile ("test.txt");
        std::ostream &out = outfile;
        out<<numberwithspelling<<endl;
    
    Return 0;
    This is fine, and I do want to create a text file, but at the same time, I would like to have that text file filled with the variable, which I can change for the different if statements. Heres what I want to do:

    Code:
    #include <iostream>
    #include <fstream>
    
    using namspace std;
    
    int main()
    {
    int number;
    char numberwithspelling[50]
    
    cout<<"Type A Number Between 1 and 20: ";
    cin>>number;
    cin.ignore();
    
    if(number==0)
    {
    cout<<"Please enter a valid number";
    }
    .....
    if(number==20)
    {
    cout<<"You Entered The Number Twenty";
    numberwithspelling<<"You Entered The Number Twenty";
    }
    
    else
    {
    cout<<"Please enter a valid number";
    }
    cin.get();
    
        std::ofstream outfile ("test.txt");
        std::ostream &out = outfile;
        out<<numberwithspelling<<endl;
    
    Return 0;
    Is there any way I can assign the value for numberwithspelling in the if statement?

    Thanks for all your help!

    FlyingIsFun1217

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Just make it a std::string instead of a char[] and then use = where you have <<.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Or if you want to keep it a char[], use strcpy() where you have numberwithspelling<<"You Entered The Number Twenty";.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Thank you both very much for the help!

    FlyingIsFun1217

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  2. Craps Program with Local Variables
    By tigrfire in forum C Programming
    Replies: 12
    Last Post: 11-09-2005, 08:01 AM
  3. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 03:46 PM
  4. Variables TO Strings
    By Okiesmokie in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 10:06 PM
  5. turning variables into strings
    By jagerhans in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2001, 01:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21