13 Replies - 473 Views - Last Post: 11 February 2014 - 07:53 PM Rate Topic: -----

#1 AlfredoMayFire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 10-February 14

Input file name help

Posted 10 February 2014 - 09:07 PM

Hey people I just started taking Computer Sciences in University and I'm just trying to make a sort of whatever program so please if there's criticism, make it constructive.
I have some code here and I wanna know how I can tell the program to accept a file open name from user input. Here's my code.(I attached it.) I have a file named Rekoil in the same folder as the source code and i inout that but the assert is failing. Please somebody any ideas or help??

Is This A Good Question/Topic? 0
  • +

Replies To: Input file name help

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Input file name help

Posted 10 February 2014 - 09:30 PM

Please post your code in code tags. Do not use attachments for code.
Was This Post Helpful? 1
  • +
  • -

#3 AlfredoMayFire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 10-February 14

Re: Input file name help

Posted 11 February 2014 - 09:41 AM

View PostSkydiver, on 10 February 2014 - 09:30 PM, said:

Please post your code in code tags. Do not use attachments for code.

#include<iostream>
#include<cassert>
#include<fstream>
#include<string>
using namespace std;
struct reviews
{
  string review,name;
}Review;
  
int main()
{
  ifstream input;
  cout << "Enter the name of the game to view its review. Be sure to type it in correctly\n";
  cin >> Review.name;
  input.open("Review.name.txt");
  assert(!(input.fail()));
  for(input >> Review.review;input >> Review.review;input >> Review.review)
    cout << Review.review;
  return 0;
}

There you go, sorry.

This post has been edited by jimblumberg: 11 February 2014 - 09:43 AM
Reason for edit:: Added missing code tags, please learn to use them properly.

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Input file name help

Posted 11 February 2014 - 10:31 AM

Your line 16 is trying to open a file name "Review.name.txt". You are not using the file name that you got from the user on line 15.
Was This Post Helpful? 1
  • +
  • -

#5 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 840
  • View blog
  • Posts: 2,472
  • Joined: 20-March 10

Re: Input file name help

Posted 11 February 2014 - 12:36 PM

Review.name is a std::string,

ifstream::open takes a null terminated const char*, in other words a C string.

here is the reference for std::string see if you can find anything that will convert std::string to a C string.

std::string reference

Regards

Snoopy.

This post has been edited by snoopy11: 11 February 2014 - 12:37 PM

Was This Post Helpful? 1
  • +
  • -

#6 AlfredoMayFire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 10-February 14

Re: Input file name help

Posted 11 February 2014 - 12:41 PM

View PostSkydiver, on 11 February 2014 - 10:31 AM, said:

Your line 16 is trying to open a file name "Review.name.txt". You are not using the file name that you got from the user on line 15.

Alright and could I get an example of how I could write code that opens a filename that the user specifies? Also, what's your opinion on the for-loop. Is it passable or would there be a better way of writing it?

View Postsnoopy11, on 11 February 2014 - 12:36 PM, said:

Review.name is a std::string,

ifstream::open takes a null terminated const char*, in other words a C string.

here is the reference for std::string see if you can find anything that will convert std::string to a C string.

std::string reference

Regards

Snoopy.

Thanks now i understand the problem. So if I use a char array as a variable instead of a string I can use the format I currently have??
Was This Post Helpful? 0
  • +
  • -

#7 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 840
  • View blog
  • Posts: 2,472
  • Joined: 20-March 10

Re: Input file name help

Posted 11 February 2014 - 12:44 PM

View PostAlfredoMayFire, on 11 February 2014 - 07:41 PM, said:

Thanks now i understand the problem. So if I use a char array as a variable instead of a string I can use the format I currently have??


No its supposed to be a C++ program use the std::string just read the reference.
Was This Post Helpful? 0
  • +
  • -

#8 AlfredoMayFire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 10-February 14

Re: Input file name help

Posted 11 February 2014 - 01:08 PM

View Postsnoopy11, on 11 February 2014 - 12:44 PM, said:

View PostAlfredoMayFire, on 11 February 2014 - 07:41 PM, said:

Thanks now i understand the problem. So if I use a char array as a variable instead of a string I can use the format I currently have??


No its supposed to be a C++ program use the std::string just read the reference.

I'm still getting the error message. Here's what I have so far...
 
#include<iostream>
#include<cstring>
#include<cassert>
#include<fstream>
#include<string>
using namespace std;
struct reviews
{
  string review,name;
}Review;
  
int main()
{
  ifstream input;
  cout << "Enter the name of the game to view its review. Be sure to type it in correctly\n";
  cin >> Review.name;
  char*cstr = new char[Review.name.length()+1];
  strcpy(cstr, Review.name.c_str());
  input.open("cstr.txt");
  assert(!(input.fail()));
  for(input >> Review.review;input >> Review.review;input >> Review.review)
    cout << Review.review;
  return 0;
}
  

Was This Post Helpful? 0
  • +
  • -

#9 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 840
  • View blog
  • Posts: 2,472
  • Joined: 20-March 10

Re: Input file name help

Posted 11 February 2014 - 01:24 PM

have you tried just

input.open(Review.name.c_str());

??

or do you wish to append '.txt' to the std::string ?

Snoopy.
Was This Post Helpful? 1
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,966
  • Joined: 19-February 09

Re: Input file name help

Posted 11 February 2014 - 01:36 PM

Hi, you have a comma between review and name making two strings.

 
struct reviews
{
  string review, name;

} Review;




Possibly you just want

string name;




Edit: sorry review has confused me.

The for loop could be a while loop.

while( input >> Review.review )
{
  cout << Review.review;
}



Perhaps, text as a variable name instead of review would be less confusing.

struct Review
{
  string name;
  string text;
};

Review review;

review.name = "";
review.text = "";


This post has been edited by #define: 11 February 2014 - 03:32 PM

Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Input file name help

Posted 11 February 2014 - 03:22 PM

View PostAlfredoMayFire, on 11 February 2014 - 03:08 PM, said:

View Postsnoopy11, on 11 February 2014 - 12:44 PM, said:

View PostAlfredoMayFire, on 11 February 2014 - 07:41 PM, said:

Thanks now i understand the problem. So if I use a char array as a variable instead of a string I can use the format I currently have??


No its supposed to be a C++ program use the std::string just read the reference.

I'm still getting the error message. Here's what I have so far...
 
#include<iostream>
#include<cstring>
#include<cassert>
#include<fstream>
#include<string>
using namespace std;
struct reviews
{
  string review,name;
}Review;
  
int main()
{
  ifstream input;
  cout << "Enter the name of the game to view its review. Be sure to type it in correctly\n";
  cin >> Review.name;
  char*cstr = new char[Review.name.length()+1];
  strcpy(cstr, Review.name.c_str());
  input.open("cstr.txt");
  assert(!(input.fail()));
  for(input >> Review.review;input >> Review.review;input >> Review.review)
    cout << Review.review;
  return 0;
}
  


It's because on line 19, you are passing the literal string "cstr.txt" which probably does not exist.
Was This Post Helpful? 0
  • +
  • -

#12 AlfredoMayFire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 10-February 14

Re: Input file name help

Posted 11 February 2014 - 03:35 PM

View Postsnoopy11, on 11 February 2014 - 01:24 PM, said:

have you tried just

input.open(Review.name.c_str());

??

or do you wish to append '.txt' to the std::string ?

Snoopy.

It works now. It was a combination of the name being a cstring and not needing the quotation marks cause that meant literally opening cstr.txt. Anyways now it reads from the file but has two logic errors.
The first one is that it skips over every other word. Example -- input = "For you're information dude". Looks like this when read by the program. "Forinformation". if you could tell there are also no spaces. Any suggestions. (Working code below.
#include<iostream>
#include<cstring>
#include<cassert>
#include<fstream>
#include<string>
using namespace std;
struct reviews
{
  string review,name;
}Review;
  
int main()
{
  ifstream input;
  cout << "Enter the name of the game to view its review. Be sure to type it in correctly\n";
  cin >> Review.name;
  char*cstr = new char[Review.name.length()+1];
  strcpy(cstr, Review.name.c_str());
  input.open(cstr);
  assert(!(input.fail()));
  for(input >> Review.review;input >> Review.review;input >> Review.review)
    cout << Review.review;
  return 0;
}
  

Was This Post Helpful? 0
  • +
  • -

#13 tarmizi_adam2005  Icon User is offline

  • جوروترا

Reputation: 245
  • View blog
  • Posts: 892
  • Joined: 18-April 09

Re: Input file name help

Posted 11 February 2014 - 05:39 PM

Hi,

Quote

Anyways now it reads from the file but has two logic errors.
The first one is that it skips over every other word. Example -- input = "For you're information dude". Looks like this when read by the program. "Forinformation". if you could tell there are also no spaces. Any suggestions


Instead of using the for loop in line 21, you could try to use the getline() function. Something like this would do:

while(getline(input, Review.review))
{
   cout << Review.review;
}


Hope this helps

This post has been edited by tarmizi_adam2005: 11 February 2014 - 05:40 PM

Was This Post Helpful? 0
  • +
  • -

#14 AlfredoMayFire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 10-February 14

Re: Input file name help

Posted 11 February 2014 - 07:53 PM

View Posttarmizi_adam2005, on 11 February 2014 - 05:39 PM, said:

Hi,

Quote

Anyways now it reads from the file but has two logic errors.
The first one is that it skips over every other word. Example -- input = "For you're information dude". Looks like this when read by the program. "Forinformation". if you could tell there are also no spaces. Any suggestions


Instead of using the for loop in line 21, you could try to use the getline() function. Something like this would do:

while(getline(input, Review.review))
{
   cout << Review.review;
}


Hope this helps

Thanks that makes the program presentable at last. Hate to be the perfectionist though the input suddenly cuts off words and finishes them in the next line. Here is an example:
Copycats of the newest, hottest thing are always going to be a part of the gamin
g industry. Its no surprise that 505 Games is attempting to jump into the multi

Any last suggestions? By the way thanks for the help I really appreciate it!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1