[Beginner] Code not compiling, library issues??
This is just the beginning of my code. My intention is for the user to input a value of scientific notation, then have the program store that as string scientificNotation. I then am trying to have the code define component.mant as the string before the 'E', and component.exp for the string after.
ERRORS:
Quote:
31 expected primary-expression before '.' token
This error occurs for every instance of structName.structMember (lines 31, 33)
Quote:
32 `ignore' undeclared (first use this function)
I'm thinking this error is a result of 'ignore' being a part of header <fstream> and the fact that I'm not reading any files?? Or....I'm totally wrong which is likely...
Quote:
35 expected primary-expression before "readCh"
35 expected `;' before "readCh"
So the program isn't assigning term "component" as struct? Or am I confused about being able to call the global struct by structName?
Quote:
44 expected primary-expression before "function"
Code:
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <cstdlib>
#include <string>
using namespace std;
//Global struct
struct component
{
int mant;
double exp;
}sciNote;
//Function Prototypes
void readCharacters
(component& function) //OUT:Data destination
{
string scientificNotation;
cout << "\t\t\tScientific Notation Calculator\n\n";
cout << "Please enter a number in scientific notation.\n";
cout << "For Example: -0.1234E20\n";
getline(cin, scientificNotation, 'E');
cin << component.mant;
ignore.scientificNotation(20, 'E');
cin << component.exp;
component readCh()
{
component function;
return function;
}
}
int main ()
{
readCharacters(component function);
return 0;
}
Re: [Beginner] Code not compiling, library issues??
Code:
cin << component.mant;
should be
Code:
cin >> function.mant;
as data goes from cin. Also component is the name of a struct not a variable. cin needs to be used with a variable. The same goes for .exp.
Code:
component readCh()
{
component function;
return function;
}
you can't have nested functions - ie a function can't have another function defined with it. Also this code is meaninless as a variables of type component is defined, not used and then its value returned from the function ??
Code:
int main ()
{
readCharacters(component function);
return 0;
}
When calling a function, you don't specify the type of a variable. This would be
Code:
int main ()
{
component function;
readCharacters(function);
return 0;
}
Also, in readcharacters()
Code:
ignore.scientificNotation(20, 'E');
this isn't valid as ignore isn't a struct of class so a member can't be referenced.
You also have logic issues with reading and parsing the input.
The code below will now compile but you will need to correct the logic issues.
Code:
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <cstdlib>
#include <string>
using namespace std;
//Global struct
struct component
{
int mant;
double exp;
};
//Function Prototypes
void readCharacters(component& function) //OUT:Data destination
{
string scientificNotation;
cout << "\t\t\tScientific Notation Calculator\n\n";
cout << "Please enter a number in scientific notation.\n";
cout << "For Example: -0.1234E20\n";
getline(cin, scientificNotation, 'E');
cin >> function.mant;
//ignore.scientificNotation(20, 'E');
cin >> function.exp;
}
int main ()
{
component function;
readCharacters(function);
return 0;
}
Re: [Beginner] Code not compiling, library issues??
Hey thanks for getting back to me. And much thanks for the compile-able program. I'm trying to mess around with c++ functions that can legally do this for me not including the letter value 'E'. The trouble is that I can only find references that seem viable in string::substr and std::string. The problem is the way that these are written are extremely confusing to me.
Here's an example of string::substr from a reference:
[code// string::substr
#include <iostream>
#include <string>
int main ()
{
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (12,12); // "generalities"
unsigned pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos); // get from "live" to the end
std::cout << str2 << ' ' << str3 << '\n';
return 0;
}[/code]
This example is from http://www.cplusplus.com/reference/s...string/substr/
Re: [Beginner] Code not compiling, library issues??
You're missed the closing ] from [code so the code tags aren't being used.
What do you find confusing?
substr extracts a string from an existing string. Its first argument is the position in the string starting at 0. Its second argument is the number of chars to extract. So in str, the 12th character is the g from generalities which is 12 chars. So str2 becomes 'generalities'. If the second argument is not present then all the chars in the string from the starting position are included.
find returns the position in the string starting from 0 where the specified char or text starts. So find("live") returns 34 (if I've counted right!) and the substr returns a string starting from position 34 to the end of the string which is 'live in details.'. If the specified text isn't found it returns a special value string::npos which can be used in a test to see if the text was found or not.