Ending a function while continuing from that point reading a file - c++

I've wrote a program which reads a textfile character for character and counts the amount of vowels that pass. However if a symbol, say "*" appears, I want the program to stop counting the vowels and start counting the non-vowels, obviously continuing from that point forward. I've written a different function and entered the following:
if (kar == '*'){
reversecounting(i);
return 0;
}
Where reversecounting is the int-function counting non-vowels.
The problem I'm having right now is that whenever it does this, the function counting the non-vowels starts at the beginning of the file. Is there a way to make it continu on from the point where the other function stopped?
I'm using the "get"-function to read from the file. Thanks in advance.
Edit: sorry for not including relevant code, I've added it now and some commentary to make it understandable
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int countnonvowels( int &k ); //prototype
int countvowels( int k ){ //start function to count vowels
ifstream input; //to open textfile
char kar; // this is the character that will be read
int countvowels = 0; //local counter of vowels
input.open ("input.txt",ios::in); //opening the textfile
kar = input.get ( ); //first obtain a character from the textfile
while ( ! input.eof ( ) ) { //reads the file untile end of file
kar = input.get ( ); //the next character in the file gets "read"
if( kar == 'e' or kar == 'a' or kar == 'i' or kar == 'o' or kar == 'u' ){ //checks whether the character is a vowel
countvowels++; //local counter goes up by one
kar = input.get(); //take the next letter and print it
cout << kar << " " << countvowels << endl;
countvowels = 0; //value becomes zero again
}//if
if (kar == '*'){ //if the character is a "*", I want the non-vowels to be counted and the k-th non-vowel to be printed
countnonvowels(k);
return 0; //this function has to stop running
}//if
} // while
input.close ( );
}//countvowels
int countnonvowels(int &k){//counts the non-vowels
ifstream input;
char kar;
int countnonvowels = 0;
input.open ("input.txt",ios::in); //open the file
kar = input.get ( );
while ( ! input.eof ( ) ) { //same while loop
kar = input.get ( );
if( kar != 'e' or kar != 'a' or kar != 'i' or kar != 'o' or kar != 'u' ){ //now i want the counter to roll if it is NOT a vowel
countnonvowels++;
cout << kar << endl;
}
if (kar == '*'){ //if a "*" appears, I want the vowels to be counted again
countvowels(k);
return 0;
}//if
} // while
input.close ( );
}//countnonvowels
int main ( ) {
countvowels(2);
return 0;
} // main
The problem here is that I get an endless loop: The file gets checked for vowels, it see's a [star sign], starts counting the non-vowels from the start of the text, see's the [star sign] again and starts counting the vowels from the start of the textfile etc...
The star-sign (shift+8) makes everything cursive apparently, I hope it's clear which one I mean.

Here is a short example given that you know how to check if the letter is a vowel or not. I'm leaving modifying it to work for a whole file to you since I suspect this is a homework question.
void countLetters(const string& line)
{
int vowels, nonVowels;
vowels = nonVowels = 0;
bool countingVowels = true;
for (int position = 0; position < line.length(); position++)
{
if (countingVowels && isVowel(line[position])
{
vowels++;
}
else if (!countingVowels && !isVowel(line[position]))
{
nonVowels++;
}
else if (line[position] == '*')
{
countingVowels = !countingVowels;
}
}
cout << vowels << " "<< nonVowels<<endl;
}

Related

Palindrome but with a scentence

So writing a palindrome with pointers and boolean. I have it working with a single word but then I began building it to work with a sentence. The problem is I am unsure how to keep the new modified sentence after making it lowercase and getting rid of the spaces for it to return whether it is or isn't a palindrome. It keeps returning the palindrome as false and when I went to check why I see that the program ignores the modification and kept the original string. I can't use "&" on the parameter as I tested it out. Any hints or takes on what I can do to keep the new modified string?
int main()
{
userInput();
return 0;
}
void userInput()
{
char str[90];
std::cout<<"Please enter a string to check if it is a palindrome: ";
std::cin.getline(str, 90);
modifyString(str);
}
void modifyString(char *string)
{
int count = 0;
for (int i=0; i<strlen(string); i++)
{
putchar(tolower(string[i]));
}
for (int i = 0; string[i]; i++)
{
if (string[i] != ' ')
{
string[count++] = string[i];
}
}
string[count] = '\0';
std::cout<<string<<std::endl;
results(string);
}
bool checkPalindrome(char *string)
{
char *begin;
char *end;
begin = string;
end = (string + strlen(string)-1);
while(begin != end)
{
if ((*begin) == (*end))
{
begin ++;
end--;
}
else
{
return false;
}
}
return true;
}
void results(char *string)
{
bool isItPalindrome;
isItPalindrome = checkPalindrome(string);
if( isItPalindrome == true)
{
std::cout<<"\nCongrats, the string is a palindrome!";
}
else
{
std::cout<<"\nThis string is not a palindrome.";
}
}
For starters this definition of main
int main()
{
userInput();
return 0;
}
does not make a sense. According to the function name main the function should perform the main task that is to output whether the entered sentence is a palindrome or not.
This for loop
for (int i=0; i<strlen(string); i++)
{
putchar(tolower(string[i]));
}
does nothing useful. It just outputs the string in the lower case.
This statement
end = (string + strlen(string)-1);
can invoke undefined behavior if an empty string was passed.
This while loop
while(begin != end)
{
if ((*begin) == (*end))
{
begin ++;
end--;
}
else
{
return false;
}
}
also can invoke undefined behavior for a string containing an even number ofo characters because after this if statement
if ((*begin) == (*end))
{
begin ++;
end--;
}
if the two adjacent characters are equal then begin after incrementing will be greater than end after its decrementing. And as a result the loop will continue its iteration.
In general the approach when the original string is changed is just a bad approach.
Your program has too many functions. It is enough to write one function that will determine whether the passed string is a palindrome or not.
Here is a demonstrative program.
#include <iostream>
#include <cstring>
#include <cctype>
bool checkPalindrome( const char *s )
{
const char *t = s + std::strlen( s );
do
{
while ( s != t && std::isspace( ( unsigned char )*s ) ) ++ s;
while ( s != t && std::isspace( ( unsigned char )*--t ) );
} while ( s != t &&
std::tolower( ( unsigned char )*s ) == tolower( ( unsigned char ) *t ) &&
++s != t );
return s == t;
}
int main()
{
const size_t N = 100;
char s[N] = "";
std::cout << "Please enter a string to check if it is a palindrome: ";
std::cin.getline( s, N );
std::cout << '\n';
if ( checkPalindrome( s ) )
{
std::cout << "Congrats, the string is a palindrome!\n";
}
else
{
std::cout << "This string is not a palindrome.\n";
}
return 0;
}
Its output might look like
Please enter a string to check if it is a palindrome: 1 23 456 6 54 321
Congrats, the string is a palindrome!
Okay, I solved it!
As one of the users on here brought up a point that my lowercase did not modify the string and only prints it out. I try my best to solve the problem and I think I found the solution and everything works perfectly fine. comment back to debug it if you like to see how it looks but what I did was create a for loop again for the lower case but made another pointer with it. here how it looks.
for (char *pt = string; *pt != '\0'; ++pt)
{
*pt = std::tolower(*pt);
++pt;
}
Now that definitely changes the string into a lower case and keeps it as a lower case.
so now the modified function looks like this and ready to take any sentence palindrome you give it. Example: A nUt fOr a jAr of tUNa. We make this all lowercase and take out space and boom palindrome and return true.
void modifyString(char *string)
{
int count = 0;
for (char *pt = string; *pt != '\0'; ++pt)
{
*pt = std::tolower(*pt);
++pt;
}
for (int i = 0; string[i]; i++)
{
if (string[i] != ' ')
{
string[count++] = string[i];
}
}
string[count] = '\0';
//take out the forward slash below to see how it looks after being modified
// std::cout<<std::endl<<string<<std::endl;
results(string);
}

Checking both ends of a string not working

I am trying to the beginning of a string and the end. If the word has an uppercase letter we change it to lowercase. If the word has a space or '"' we erase the character. The first recursive call it should check and see that the end of the string has a capital letter and it should change it to lowercase. However when I output word[word.size()] it outputs a blank space, but it I output word[word.size() - 1] it will output the letter that I am looking for. I wasn't sure what the blank space is and how I should handle it as I don't want it in my string because it is causing comparison issues.
bool checkPalindrome(string word){
if (isupper(word[0]))
{
word[0] = tolower(word[0]);
}
if (isupper(word[word.size()]))
{
word[word.size()] = tolower(word[word.size()]);
}
//check if there is a space or "" if there is then delete that position from the string
if (word[0] == ' ' || word[0] == '"')
{
word.erase(1);
}
if (word[word.size()] == ' ' || word[word.size()] == '"')
{
word.pop_back();
}
if (word.size() > 1)
{
if (word[0] == word[word.size()])
{
word = word.substr(1, word.size() - 2);
return checkPalindrome(word, count);
}
else
{
return false;
}
}
else
{
return false;
}
int main()
{
ifstream inFile;
bool check = false;
string temp = "";
int count = 0;
vector<string> vect;
//Reading from a file line by line
inFile.open("words.txt");
if (inFile.is_open())
{
while (getline(inFile, temp))
{
vect.push_back(temp);
}
}
inFile.close();
for (auto i = 0; i < vect.size(); i++)
{
count = vect[i].size();
check = checkPalindrome(vect[1], count);
if (check == true)
{
cout << vect[i] << ", is a palindrome!\n";
}
else
{
cout << vect[i] << ", is not a palindrome.\n";
}
}
} return 0;
If the size of the string is 4, then there are only 4 elements: 0, 1, 2, and 3. There is no fifth element, so you cannot access element number four.
If a string's length is five:
Zero is the first element.
One is the second element.
Two is the third element.
Three is fourth element.
The fifth element is, of course Leeloo, which is not a character in the string. If a string's length is four, you should not attempt to access the fifth element (at least, not without her permission).
Ecto gamat.

Getline Randomly Stops Working

I am trying to write a very basic program to read in a CSV and display it. The function, as you can see below, is a very basic one. It uses getline to read in each row (until the newline chracter) before displaying each cell. It was working fine and I was finetuning the loop to display each cell when getline simply stopped working. Without changing any of the code to do with getline, I compiled it and it would not read from the file. row is always empty. However, the ifstream is valid because the "test" string (which I inserted in response) reads in from sheet fine. Can anyone help me?
PS: I have had similar problems with getline before. Is it something to do with my system? It seems to work on and off
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
ifstream sheet( "StockTake.csv", ios::app );
if ( ! sheet.is_open() )
{
cout << "Cannot Open File!\n";
return 0;
}
else
{
cout << "Opened File\n";
}
//Now print each row (find the newline chracter at the end of each line)
/*string test;
sheet >> test;
cout << test;*/
for ( string row; ! sheet.eof(); getline(sheet, row, '\n') )
{
if (row == "")
{
cout << "Bad Read. Exiting\n";
return 0;
}
//Print out each row with a tab space between each cell
string cell;
//find the beginning and the end of each cell
for (int i = 0, j = 0; ; )
{
/*Checks if the cell is enclosed in quotes
The first time, j == i hence the -2 and +2
the +2 is required to "skip" out the apostrophes if
they are found*/
if (row.at(i) == '"')
{
i++;
j = i-2;
do
{
j = row.find('"', j+2);
} while (row.at(j+1) == '"');
/*Check for the "" apostrophe sign*/
}
else
{
j = row.find(',', i);
}
/*Print the Cell*/
if (j == string::npos) //if this is the last cell
{
cell = row.substr(i, row.size() - i);
}
else if ( j-i != 0)
{
cell = row.substr(i, j-i);
}
else
{
cell = "";
}
/*Check for the "" apostrophe sign, and replace
with " for each instance*/
if ( cell.find("\"\"", 0) != string::npos )
{
int pos = -2; //must start at zero
do
{
//Skips out the "current" apostrophe
//if there are more than one
pos = cell.find("\"\"", pos+2);
cell = cell.substr(0, pos) +
cell.substr(pos+1, cell.size()- (pos+1) );
} while (cell.find("\"\"", pos+2) != string::npos);
}
/*Display the Cell, only the space will be displayed
if the cell is empty*/
cout << cell << " ";
/*Find the next cell*/
if ( row.at(j) == '"' )
{
i = j+2;
}
else
{
i = j+1;
}
}
cout << "\n"; //Print newline character at the end of each line
}
return 0;
}

EOF AND GETCHAR to end loop

I'm new in C programing language . I have a question how can I end loop in windows.
#include<stdio.h>
#include<conio.h>
int main() {
printf("enter ur palindrome");
int i[100], c, d = 0, n = 0;
c = getchar();
while (c != '\n') {
i[d] = c;
d++;
}
loop:
while (d != 0) {
if ((i[0 + n] != i[d - n])) {
n++;
goto loop;
}
printf("this is not a palindrome");
break;
}
printf("this is a palindrome");
return (0);
}
I HAVE TRIED ALMOST EVERYTHING CTRL+Z, CTRL+C, CTRL+D, REPLACING '\n' WITH EOF
and many more thing. Nothing worked for me. I'm using CodeBlocks on Windows 10.
Is there some other way of writing such type of program other then getchar and eof.
Rather than using goto, use continue; to repeat the loop.
However, there are a number of other problems with the posted code.
The array `int i[100]` is never terminated with a NUL byte ('\0')
An array of char not int should be used.
this loop: `while (d != 0) will never exit,
because (if the loop is ever entered)
the `d` variable is never changed within the loop
Here is my suggested code:
caveat: not thoroughly tested
#include <stdio.h>
//#include<conio.h> <-- not used, so do not include
#include <string.h>
#define MAX_LENGTH (100)
int main( void )
{
int d;
int n;
char i[MAX_LENGTH];
printf("enter ur palindrome\n"); // <-- \n so will immediately print
if ( NULL != fgets( i, MAX_LENGTH, stdin ) )
{
if( strlen( "\n" ) < strlen( i ) )
{ // then, some string entered
// remove any trailing '\n'
char *newline = strstr( i, "\n" );
if( newline )
{ // then '\n' found
*newline = '\0';
} // end if
d = strlen( i );
for( n=0; (d-n) >= n; n++ )
{
if( i[0 + n] != i[d - n] )
{ // then no match
printf("this is not a palindrome");
break;
} // end if
} // end for
if( (d-n) < n )
{ // then palindrome
printf("this is a palindrome");
} // end if
}
else
{
printf( "nothing entered\n");
} // end if
} // end if
return (0);
} // end function: main
you probably want to take a look at this section again
c=getchar();
while(c!= '\n') // Value of c never altered, hence it'll never break
{ i[d]=c;
d++;
}
and yes, the other loop
loop: // not required
while ( d!=0 ) // can't see change in d within this body, is it n ?
{
if((i[0+n] != i[d-n]))
{
n++;
goto loop; // not required
continue; //will do
}
printf("this is not a palindrome");
break;
}
and you'd actually get an extra message saying
this is a palindrome
after printing
this is a not palindrome
which I suppose is not what you want.
In that loop you need to again read next character so need to add getchar() in that loop
c=getchar();
while(c!= '\n')
{
i[d]=c;
d++;
c=getchar();
}
Here's a different way to write that piece of code
#include <stdio.h>
#include <string.h>
int main()
{
char *word;
int i;
int n;
printf( "enter ur palindrome" );
scanf("%[^\n]", word); // reads input all the way to the new line
n = strlen( word );
for ( i = 0; i < n; i++ ) {
if ( word[ i ] != word[ n-1 - i ] ) {
break; /* for */
}
}
if ( i == n ) {
printf( "This is a palindrome");
} else {
printf( "This is not a palindrome" );
}
return 0;
}

Polynomial Calculator

I'm doing a polynomial calculator and i'll need some help as i'll progress with the code.
For now I made only the polinom class which i represented it as a linked list with terms and some functions(only read and print the polynomial functions for now).
Here's the main program which for now only read a polynomial and prints it:
#include "polinom.h"
int main()
{
polinom P1;
bool varStatus = false;
char var = '\0', readStatus = '\0';
cout << "P1 = ";
P1.read(readStatus, var, varStatus); // i don't need readStatus yet as i haven't implemented the reset and quit functions
cout << "\n\nP = ";
P1.print(var);
getch();
return 0;
}
And the header file polinom.h:
#ifndef _polinom_h
#define _polinom_h
#include <iostream>
#include <list>
#include <cstdlib>
#include <cctype>
#include <cstdio>
#include <conio.h>
using namespace std;
class polinom
{
class term
{
public:
int coef;
int pow;
term()
{
coef = 1;
pow = 0;
}
};
list<term> poly;
list<term>::iterator i;
public:
bool printable(char c)
{
return (
((int(c) > 42 && int(c) < 123) || isspace(c)) && int(c) != 44 && int(c) != 46 && int(c) != 47 &&
int(c) != 58 && int(c) != 59 &&
int(c) != 60 && int(c) != 61 && int(c) != 62 && int(c) != 63 && int(c) != 64 && int(c) != 65 &&
int(c) != 91 && int(c) != 92 && int(c) != 93 && int(c) != 95 && int(c) != 96
);
}
void read(char &readStatus, char &var, bool &varStatus)
{
term t; // term variable to push it into the list of terms
char c, lc, sign; // c = current char, lc = lastchar and sign the '+' or '-' sign before a coefficient
int coef, pow; //variables to pass the coef and power to term t
bool coefRead = false, powRead = false; //reading status of coef and power
while (c != '\r') { //we read characters until carriage return
c = getch(); // get the new imputed char
if (tolower(c) == 'r' || tolower(c) == 'q') { //if the user inputed r or q we reset the input or quit the program
readStatus = c; //pass current char value to readStatus so the program will know what to do next
return; //aborting the reading process
}
else
{
if (printable(c)) cout << c; //print on screen only the correct characters
if (!coefRead && !powRead) //we set term coef to the inputed value
{
if (isdigit(c)) {
if (isdigit(lc)) coef = coef * 10 + int(c); //if the last char was also a digit we multiply the last value of coef by 10 and add current char
else {
if (sign == '-') coef = -(int(c));//if the current coef has '-' before we set coef to it's negative value
else coef = int(c); //this means a new term's coef is read
}
if (!isdigit(c) && isdigit(lc)) coefRead = true; //if the last char was a digit and we reached the var name we stop reading the coefficient
}
else if (coefRead && !powRead) //after coefficient is read we get the term's varname and power
{
if (isdigit(c)) { // just like in the case with coefficient we read the power until the current char is not a digit
if (isdigit(lc)) pow = pow * 10 + int(c);
else pow = int(c);
}
else if (isalpha(c) && isdigit(lc) && !varStatus) { //if the last char was a digit and the current not we reached the var name
var = c; //also even though the variable is inputed more than once we save it only once
varStatus = true; //we mark the var name as read
}
else {
if (isdigit(lc)) powRead = true;
}
}
else {
if (c == '+' || c == '-') { // if a sign was inputed it means a new term is coming and we push the current term to the list and reset
t.coef = coef; // coefRead and powRead so we can read another term
t.pow = pow;
poly.push_back(t);
sign = c;
coefRead = false;
powRead = false;
}
}
lc = c; // we save the last character
}
}
}
void print(char var)
{
for ( i=poly.begin() ; i != poly.end(); i++ ) { //going through the entire list to retrieve the terms and print them
if (i == poly.end() - 1) { // if we reached the last term
if (*(i->pow == 0) //if the last term's power is 0 we print only it's coefficient
cout << *(i->coef);
else
cout << *(i->coef) << var << "^" << *(i->pow); //otherwise we print both
}
else {
if (*(i->coef > 0) //if the coef value is positive
cout << *(i->coef) << var << "^" << *(i->pow) << " + "; //we also add the '+' sign
else
cout << *(i->coef) << var << "^" << *(i->pow) << " - "; // otherwise we add '-' sign
}
}
}
};
#endif
EDIT
All compile errors fixed now thanks to JonH, but the read function is not working as the input characters aren't correctly inserted into the list. I know it may be trivial for you guys, but it would be great if you help me out.
Thanks!
I found MANY missing curly braces and closing parens all throughout your code. After having spent several minutes fixing at least 10 of these, I thought you would be better served if I helped you to learn to fish, rather than giving you fish for tonight's dinner.
Your code is written like a stream of consciousness. As you are building your code your mind jumps around, thinking of other things you need to build and new requirements introduced by whatever you just wrote. When you think of these things, you go write them and come back to where you were. Before you know it, you have written hundreds of lines of code by jumping around, writing bits here and there. The problem with this is that you can't possibly keep juggling sections of code like this without missing little syntax bits along the way.
You should take a more iterative approach to writing code. How exactly you do this will come with experience, but here's some guidance:
Start by stubbing out a class declaration with a few (preferably 1) core methods and member variables.
Compile. You'll get linker errors and the like, but you shouldn't get any syntax errors like missing parens or semicolons. Fix any you do find before moving on.
Implement the methods/functions you just stubbed. Compile & fix non-linker errors.
As you think of minor or dependant requirements that came up during the above steps, write comments in your code, like // TODO: Implement bool DoTheThing(int); But don't implement them yet.
Loop back to step 1, keeping the scope of what you're working on as limited and fundamental as possible. Never move beyond a compilation step without a clean compile.
Repeat until you have implemented everything. You might compile 50 times or more during this process.
Your fundamental problem is that you wrote a bunch of code down without testing it piece by piece, without thinking about it. When you write as a beginner, you should try adding one little bit at a time and making sure it compiles. Even as an advanced programmer, modularization is an extremely important part of the design and code-writing process.
That said, here are a few tips about your posted code in particular:
Your function printable is ugly as sin, and therefore impossible to debug or understand.
The number of nested if statements is indicative of design flaws.
You're missing an end brace on your if (isdigit(c)) statement.
Declaring (and especially initializing) multiple variables on the same line is bad form.
Those compile errors surely has a line number associated to them in the error message. Have you tried looking at the line indicated to see what is missing? If that does not help, please post the complete error output from the compiler so that we can se what the error is.
You are missing a few curly braces in your read function.
I redid it here:
void read(char &readStatus, char &var, bool &varStatus)
{
term t; // term variable to push it into the list of terms
char c, lc, sign; // c = current char, lc = lastchar and sign the '+' or '-' sign before a coefficient
int coef, pow; //variables to pass the coef and power to term t
bool coefRead = false, powRead = false; //reading status of coef and power
while (c != '\r') { //we read characters until carriage return
c = getch(); // get the new imputed char
if (tolower(c) == 'r' || tolower(c) == 'q')
{ //if the user inputed r or q we reset the input or quit the program
readStatus = c; //pass current char value to readStatus so the program will know what to do next
return; //aborting the reading process
}
else
{
if (printable(c))
cout << c; //print on screen only the correct characters
if (!coefRead && !powRead) //we set term coef to the inputed value
{
if (isdigit(c))
{
if (isdigit(lc))
coef = coef * 10 + int(c); //if the last char was also a digit we multiply the last value of coef by 10 and add current char
else
{
if (sign == '-')
coef = -(int(c));//if the current coef has '-' before we set coef to it's negative value
else
coef = int(c); //this means a new term's coef is read
} //end else
}//end if isdigit(c)
if (!isdigit(c) && isdigit(lc))
coefRead = true; //if the last char was a digit and we reached the var name we stop reading the coefficient
} //end if
else if (coefRead && !powRead) //after coefficient is read we get the term's varname and power
{
if (isdigit(c))
{ // just like in the case with coefficient we read the power until the current char is not a digit
if (isdigit(lc))
pow = pow * 10 + int(c);
else
pow = int(c);
}
else if (isalpha(c) && isdigit(lc) && !varStatus)
{ //if the last char was a digit and the current not we reached the var name
var = c; //also even though the variable is inputed more than once we save it only once
varStatus = true; //we mark the var name as read
}
else
{
if (isdigit(lc))
powRead = true;
}
} //end else if
else
{
if (c == '+' || c == '-')
{ // if a sign was inputed it means a new term is coming and we push the current term to the list and reset
t.coef = coef; // coefRead and powRead so we can read another term
t.pow = pow;
poly.push_back(t);
sign = c;
coefRead = false;
powRead = false;
}
}
lc = c; // we save the last character
} //end else
} //end while
} //end function
EDIT
I also fixed the print function:
void print(char var)
{
for ( i=poly.begin() ; i != poly.end(); i++ ) { //going through the entire list to retrieve the terms and print them
if (i == poly.end()) { // if we reached the last term
if (i->pow == 0) //if the last term's power is 0 we print only it's coefficient
cout << i->coef;
else
cout << i->coef << var << "^" << i->pow; //otherwise we print both
}
else {
if (i->coef > 0) //if the coef value is positive
cout << i->coef << var << "^" << i->pow << " + "; //we also add the '+' sign
else
cout << i->coef << var << "^" << i->pow << " - "; // otherwise we add '-' sign
}
}
}