How to do things without declaring global identifiers or I'm Stuck!

Hey guys I have a project I'm working on for a beginning CS class.
This forum has been ridiculously helpful in the past so I'm hoping I can learn a little bit more today.


Now in this new assignment it's all about strings, functions,and fstream.
The thing is I can't use any global identifiers so I have to rely on functions and passing parameters by reference.

This is my sample input:

4 52 the     OnCE and futuRE       carPENTER
2 56     i   nEvEr   KNeW   you
3 4       liFe
4 8   fEbruAry sEVEN



This is the sample output:
Please enter name of input file
songfile

SONG TITLE                                   LENGTH      TOT TIME
The Once And Future Carpenter                 04:52      00:04:52
I Never Knew You                              02:56      00:07:48
Life                                          03:04      00:10:52
February Seven                                04:08      00:15:00



This is my code so far:
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
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

void getfilename(string&);
void reformat(string&);


int main()
{
  cout << "Name     Section 1003     Assignment #9" << endl;

  ifstream input;
  getfilename ("input", filename);
  input.open(filename.c_str());

  cout << "SONG TITLE                                   LENGTH      TOT TIME";

  input >> filename;
  while (input)
    {

      while (input != /n)


  return 0;
}

  void getfilename(string&) // Prompting the user to enter the name of the file.                                                                                                               
{
  cout << "Please enter name of input file" << endl;
  cin >> filename;
}
  void reformat (string& word) // Reformatting the words so that the first letter is capitalized and the rest are lower case.                                                                                                                               
{
  word[0] = toupper(word[0]);
  for (int i=1; i<word.length(); i++)
    word[i] = tolower(word[i]);
}




I don't understand how I'm supposed to read the input without declaring a global variable for it to exist in...
Any suggestions?
The work is in progress so somethings may not make sense...
Global variables are the variables that have program scope, i.e. declared above your int main(). Is there any reason you can't declare them within the main() function?

Second, when you create functions, have the parameters passed be the variable types you want to work with. If you need to manipulate them directly, pass parameters by reference...

http://www.cplusplus.com/doc/tutorial/functions2/

So for example...

1
2
void CleanUpThisMessyFileLine(string& aFileLine)
{ /*do stuff to aFileLine to fix it*/ }
Last edited on
You are definately missing some code between lines 21 and line 26 and you haven't declared string filename.

You don't show how you call reformat().

In terms of getfilename, you've declared it with:
void getfilename(string&);
defined it with:
void getfilename(string&)
and call it with:
getfilename ("input", filename);

Do you see a problem with the number of parameters?

You'll want to do it this way:
decalaration:
void getfilename(string&);
definition:
void getfilename(string& filename)
calling:
getfilename(filename);

Then ensure that filename is defined as a string somewhere in the main function.
Last edited on
@HellfireXP

Yes it's just part of the assignment, we can't declare global variables so yes no variables in the main function.

@Stewbond

Thanks mang! But I can't define string in the main function.

I think I'm just going to read the values within each function and pass by reference.
Momojams wrote:
we can't declare global variables so yes no variables in the main function.

Global variables are those defined outside the main() function.

But I can't define string in the main function.

But you have already defined ifstream input; (which is a variable) in main.

I think you may have misunderstood (or merely mis-stated) the requirements for the assignment.
@Chervil

Ya you're right, I was confusing global with local variables.
I'm still trying to figure this out; I just feel so lost right now...

Here's a slightly updated version:
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

void getfilename(string&);
void reformat(string&);
double gettotaltime(double&);

int main()
{
  double songtime;
  string songtitle;
  double totaltime;
  string filename;
  int c = 0;

  cout << "Name     Section 1003     Assignment #9" << endl;
  
  ifstream input;
  getfilename ("input", filename);
  input.open(filename.c_str());

  cout << "SONG TITLE                                   LENGTH      TOT TIME";

  input >> filename;
  while (input != /n)           // Loop
    {
      cin >> songtime;         // Take in song time, figure out a way to put back 0
      if (songtime > 10)
	{ 
	  cin.putback (c);
	}

      cin >> songtitle;             // Reformat song title
      reformat (songtitle);

      gettotaltime(totaltime);       // Calculate total of all song tracks
    }

	cout << songtitle << songtime << totaltime << endl;
  return 0;
}

  
  void getfilename(string& filename) // Prompting the user to enter the name of the file.
  {
  char filename;
  cout << "Please enter name of input file" << endl;
  cin >> filename;
  }
  
  void reformat (string& word) // Reformating the words so that only the first letter is capitalized and the rest are lower case.   
  {
  word[0] = toupper(word[0]);
  for (int i=1; i<word.length(); i++)
    word[i] = tolower(word[i]);
  }
  
double gettotaltime (double& totaltime)
{
  double songtime;
  songtime = songtime + totaltime;
}


I need to figure out a way to add a "0" in front of song lengths so I can display it in a proper fashion.
I think what my problem is that I need to read over how strings work and how to call functions properly.
Last edited on
It looks like you are using double songtime; to hold the duration of the song.

But, based upon the sample input file, you need two integer variables, one each for minutes and seconds. When you output these, you can use setw() and setfill(), to set the size of the printed value and whether to pad it with spaces (default) or some other character ('0' would be useful). You can also use left or right to put the padding on the front or back of the value.

When calculating the total time, you probably should convert the minutes and seconds to a single seconds value, which can then be added to the total. Outputting the result involves some manipulation to find how many hours, minutes and seconds that would make.
@Chervil

I took your comments into consideration
Check it out:
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

void getfilename(string&);
void reformat(string&);
int gettotaltime(int&);

int main()
{
  int songtimeminutes;
  int songtimeseconds;
  string songtitle;
  int totaltime;
  string filename;
  int c = 0;

  cout << "Name    Section 1003     Assignment #9" << endl;
  
  ifstream input;
  getfilename (filename);
  input.open(filename.c_str());

  cout << "SONG TITLE                                   LENGTH      TOT TIME";

  input >> filename;
  while (input filename != '\n')           //End of line Loop
    {
      cin >> songtimeminutes >> songtimeseconds;         // Take in song time, figure out a way to put back 0
      if (songtimeminutes > 10 || songtimeseconds > 10) // Or try for (int songtimeminutes = 0; m < 10; "0"
	{ 
	  cin.putback (c);
	}

      cin >> songtitle;             // Reformat song title
      reformat (songtitle);

      gettotaltime(totaltime);       // Calculate total of all song tracks
    }

  cout << songtitle << songtimeminutes << songtimeseconds << totaltime << endl;
  return 0;
}

void getfilename(string& filename) // Prompting the user to enter the name of the file.
{
  cout << "Please enter name of input file" << endl;
  cin >> filename;
}
  
void reformat (string& word) // Reformating the words so that only the first letter is capitalized and the rest are lower case.   
{
  word[0] = toupper(word[0]);
  for (int i=1; i<word.length(); i++)
    word[i] = tolower(word[i]);
}
  
int gettotaltime (int& totaltime)
{
  int songtimeminutes;
  int songtimeseconds;
  songtimeminutes*60;
  totaltime = songtimeminutes + songtimeseconds;
}


But I'm still getting this compiling error:
hw09.cpp: In function ‘int main()’:
hw09.cpp:35: error: no match for ‘operator!=’ in ‘filename != '\012'’
Last edited on
27
28
29
  input >> filename;
  while (input filename != '\n')           //End of line Loop
    {
This doesn't make sense - can you see why?
I get the impression that line 33, cin.putback (c); is attempting to affect the appearance of the output during the stage where you are reading the input.

If I understood the intention, that would be a misguided approach. A cleaner method is to read the input into your variables, and after that, perform whatever tidying-up of the words, and arithmetic with the numbers.

After that, create the output. At that stage, you will be concerned with the appearance, and could use setfill() and setw() to automatically add the leading zero where required.
Last edited on
@ L B
Yes I see :)

@ Chervil
Got it.

After much editing is my updated code:
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

void getfilename(string&); // function to prompt for file name
void reformat(string&); // function to format the words to approipate capital and lower case
int gettotaltime(int&, int&, int&);  // function to get the total time of all song tracks
void getridofspace(string&);  // function to get rid of the spaces within song titles
int formattime(int, int);   // function to format the length of the songs in  the hh:mm:ss format

int main()
{
  int songtimehours;
  int songtimeminutes;
  int songtimeseconds;
  string songtitle;
  int totaltime;
  string filename;

  cout << "Name    Section 1003     Assignment #9" << endl << endl;
  
  ifstream input;
  getfilename (filename);
  input.open(filename.c_str());

  cout << "SONG TITLE                                   LENGTH      TOT TIME" << endl;

  input >> songtimeminutes;

  while (input)                              //End of file line Loop
    {
      input >> songtimeseconds;   
      
      getline(input, songtitle);
                                          
      reformat(songtitle);
      getridofspace(songtitle);

      cout << songtitle << endl;

      formattime(songtimeminutes,songtimeseconds);
      gettotaltime(songtimehours,songtimeminutes,songtimeseconds);      // Calculate total of all song tracks
      
      input >> filename;
          }
  return 0;
}

void getfilename(string& filename) 
{
  cout << "Please enter name of input file:" << endl;
  cin >> filename;
}
  
void reformat (string& word) 
{
  word[0] = toupper(word[0]);
  for (int i=1; i<word.length(); i++)
    word[i] = tolower(word[i]);
}
  
int gettotaltime (int& songtimehours, int& songtimeminutes, int& songtimeseconds)
{
  songtimeminutes = (songtimeminutes + (songtimeseconds/60)) % 60;
		     songtimeminutes = songtimeminutes %60;
		     songtimehours = songtimeminutes/60;
		     cout <<  songtimehours << ":" << songtimeminutes <<":"  << songtimeseconds;
}

void getridofspace (string& songtitle)
{
  for (int i=0; i<songtitle.length(); i++)
    {
      if (songtitle[i] == ' ')
	{
	  songtitle.erase(i,1);
	}
    }
}

int formattime (int songtimeminutes, int songtimeseconds)
{
  if (songtimeminutes < 10 && songtimeseconds < 10)
    {
      cout << "0" << songtimeminutes << ":" << "0" << songtimeseconds;
    }
  if (songtimeminutes >= 10 && songtimeseconds < 10)
    {
    cout << songtimeminutes << ":" << songtimeseconds;
    }
}


Here is my output:



Name   Section 1003     Assignment #9

Please enter name of input file:
songfile
SONG TITLE                                   LENGTH      TOT TIME
the  onceandfuture   carpenter
0:4:52  i never knew you
0:4:56   life
04:040:4:4 februaryseven
04:080:4:8


Things that I need to fix:
* capital letters
* proper spacing
* proper formatting
* calculating time

Any help would be much appreciated :)
There are very many ways to approach this. You could by all means continue to refine the code which you have.

I'll just comment briefly. There's a while loop starting at line 31.
I would say that line 29, input >> songtimeminutes; should be within this loop, not outside it.

At the end of the loop, line 45, input >> filename; does not seem at all appropriate, and this line should be deleted.

As for the word formatting, well, if you first call the routine to remove the multiple spaces, after that, it will be a more straight forward matter to properly capitalise the text. The very first character of songtitle, and every character which is immediately preceded by a space, should be upper case.

On the other hand, it might be simpler to use a stringstream. This allows the convenient reading of each word, one at a time, and you can then capitalise the current word, before concatenating them back together, with a single space between each word.

Last edited on
Topic archived. No new replies allowed.