6 Replies - 625 Views - Last Post: 25 February 2015 - 01:10 PM Rate Topic: -----

#1 Torm04   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 14-June 14

expected primary-expression before... help

Posted 25 February 2015 - 09:03 AM

Hello, I am a little confused as to why it is giving me the error:
C:\Users\Brendan\Desktop\C++\Chapter 10 Test 4.cpp In function 'int main()':
14 25 C:\Users\Brendan\Desktop\C++\Chapter 10 Test 4.cpp [Error] expected primary-expression before 'line1'
14 39 C:\Users\Brendan\Desktop\C++\Chapter 10 Test 4.cpp [Error] expected primary-expression before 'line2'
14 53 C:\Users\Brendan\Desktop\C++\Chapter 10 Test 4.cpp [Error] expected primary-expression before 'line3'

Here is the code:
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;

//Function Prototypes//
int charString(string, string, string);
string reverseString(string, string, string);
//------------------//

int main()
{
   reverseString(string line1, string line2, string line3);
	
}

string charString()
{
	ifstream inFile;
	// Holds the strings for each line.
	string line1;
	string line2; 
	string line3; 
	 // Holds the lenth of each of the lines.
	int length1 = 0; 
	int length2 = 0; 
	int length3 = 0;
	
	inFile.open("testdata.txt"); //Opens test .txt file
	//Gets the full string line.
	getline(inFile, line1);
	getline(inFile, line2);	
	getline(inFile, line3);
	//Gets the strings length.
	
    for(int i = 0; i < line1.length(); i++) //Determines isalpha string length for line 1
    {
    	if(isalpha(line1[i]))
    	{
    		length1++;
		}
	}
	 for(int i = 0; i < line2.length(); i++) //Determines isalpha string length for line 2
    {
    	if(isalpha(line2[i]))
    	{
    		length2++;
		}
	}
	 for(int i = 0; i < line3.length(); i++) //Determines isalpha string length for line 3
    {
    	if(isalpha(line3[i]))
    	{
    		length3++;
		}
	}
	cout << length1 << endl;
	cout << length2 << endl;
	cout << length3 << endl;
	
	return line1, line2, line3;
}

string reverseString(string line1, string line2, string line3)
{
	
	cout << "Here is the String the correct way." << endl;
	cout << line1 << endl << line2 << endl << line3 << endl;
	cout << "Here is the String backwards." << endl;
	cout << "Nothing here yet ://>/>" << endl;
	
}



Thanks to anyone who can help.

Is This A Good Question/Topic? 0
  • +

Replies To: expected primary-expression before... help

#2 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 792
  • View blog
  • Posts: 2,873
  • Joined: 31-December 10

Re: expected primary-expression before... help

Posted 25 February 2015 - 09:22 AM

You're not calling the reverseString function correctly. You would declare strings, then pass them to the function. The function also returns a string which you're currently ignoring.

*EDIT*:
What is the purpose of the charString() function? This is where comments can really come in handy.

This post has been edited by vividexstance: 25 February 2015 - 09:23 AM

Was This Post Helpful? 1
  • +
  • -

#3 Torm04   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 14-June 14

Re: expected primary-expression before... help

Posted 25 February 2015 - 09:28 AM

View Postvividexstance, on 25 February 2015 - 10:22 AM, said:

You're not calling the reverseString function correctly. You would declare strings, then pass them to the function. The function also returns a string which you're currently ignoring.

*EDIT*:
What is the purpose of the charString() function? This is where comments can really come in handy.

Yeah, sorry about not adding comments below. The charString function is basically for getting the number of characters from a .txt file. I wasn't doing anything with it quite yet because I was going to use it later on in the assignment.

I guess I am still a little shaky on function returns with multiple values. Could you give me some kind of an example on how to do it?

This post has been edited by Torm04: 25 February 2015 - 09:28 AM

Was This Post Helpful? 0
  • +
  • -

#4 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 792
  • View blog
  • Posts: 2,873
  • Joined: 31-December 10

Re: expected primary-expression before... help

Posted 25 February 2015 - 09:49 AM

For one thing, you can only return a single value. You use the return keyword and what variable/value you want to return. Whatever is returned should match the function's signature.

int sum(int a, int B)/>
{
   return a + b;
}

int main(void)
{
    cout << "sum(10, 20) = " << sum(10, 20) << endl;
    int val = sum(100, 200);
}


Was This Post Helpful? 0
  • +
  • -

#5 Torm04   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 14-June 14

Re: expected primary-expression before... help

Posted 25 February 2015 - 09:57 AM

I see, so if I am only able to return one value from a function, would I have to use something sucj as functions?
Thanks for the example and help so far by the way.
Was This Post Helpful? 0
  • +
  • -

#6 Torm04   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 14-June 14

Re: expected primary-expression before... help

Posted 25 February 2015 - 10:29 AM

Long day... sorry about that confusing reply.
Edited Version -v

I see, so if I am only able to return one value from a function, would I have to use something such as pointers?
Thanks for the example and help so far by the way.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg   User is offline

  • member icon

Reputation: 5549
  • View blog
  • Posts: 17,176
  • Joined: 25-December 09

Re: expected primary-expression before... help

Posted 25 February 2015 - 01:10 PM

Pointers would work but since this seems to be C++ think about using references instead, or perhaps a structure or class instead of the discrete variable.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1