Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: String Spaces,New Lines and Inserting Text  (Read 46452 times)

0 Members and 1 Guest are viewing this topic.

Teroro

    Topic Starter


    Greenhorn

    • Experience: Beginner
    • OS: Unknown
    String Spaces,New Lines and Inserting Text
    « on: October 28, 2021, 02:02:07 PM »
    So. I have a string that I create within a file and I want to put spaces,new lines and add some input in the code but it doesn't work.

    I tried putting
    Code: [Select]
    "" between the text as I saw online but it doesn't work. And the new lines I didn't found on the internet.
    Im trying to create a file with this code in it and this is how I got it to even create the file.
    Code: [Select]
    set /p CPPFileName=
    echo ^#include^<fstream^>usingnamespacestd^;ifstreamfin(^"^")^;ofstreafout(^"^")^;int main^(^)^{inta^,b^;fin^>^>a^>^>b^;fout^<^<a^*b^;^} > "%CPPFileName%"
    And this is the output
    Code: [Select]
    #include<fstream>usingnamespacestd;ifstreamfin("");ofstreafout("");int main(){inta,b;fin>>a>>b;fout<<a*b;}
    But I want this to be the output using Spaces And New lines
    Code: [Select]
    #include<fstream>
    usingnamespace std;
    ifstream fin("");
    ofstreaf out("");
    int main()
    {
    int a,b;
    fin>>a>>b;
    fout<<a*b;
    }
    And also about Inserting text.
    I have 2 Inputs and the name of the strings are
    Code: [Select]
    File1Name and
    Code: [Select]
    File2Name Is there a way I could add these strings in the code string I want to output?
    Like
    Code: [Select]
    ^#include^<fstream^>usingnamespacestd^;ifstreamfin(^"%File1Name%^")^;ofstreafout(^"%File2Name%^")^;int main^(^)^{inta^,b^;fin^>^>a^>^>b^;fout^<^<a^*b^;^}
    If you could help me it would be awesome!

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: String Spaces,New Lines and Inserting Text
    « Reply #1 on: October 28, 2021, 05:36:50 PM »
    Not sure why you have all that additional escaping. To resolve your initial problem you can do this.

    Code: [Select]
    set /p CPPFileName=
    (
    echo #include^<fstream^>
    echo usingnamespace std;
    echo ifstream fin(""^);
    echo ofstreaf out(""^);
    echo int main(^)
    echo {
    echo int a,b;
    echo fin^>^>a^>^>b;
    echo fout^<^<a*b;
    echo }
    )> "%CPPFileName%"

    I am not sure I am understanding your second issue.  I don't see any code where your are assigning any variables named File1Name or File2Name so I am not sure if you want the values of the variables or you literally want to output %File1Name%.

    Teroro

      Topic Starter


      Greenhorn

      • Experience: Beginner
      • OS: Unknown
      Re: String Spaces,New Lines and Inserting Text
      « Reply #2 on: October 29, 2021, 02:01:12 AM »
      Thanks again for the help!
      I got it to work after playing with the strings for some more but thanks anyways