Hello, I am trying to put finishing touches on a program I have been working on. I stopped messing with it awhile ago and decided to return and polish it a little.

Oh yeah, I am trying to make it so when it returns the 5 long number (or word actually lol) that it does it and makes it smoother looking. For example 123 would return (weird symbol)(weird symbol)321, but I want it to just be 321. and instead of say (K)(K)321 it would return 321(empty)(empty)

Here is the actual code I have been working on.

#include <iostream>
#include <conio.h>
#include <string>
#include <sstream>
using namespace std;

char n0,n1,n2,n3,n4;
string mystr;

int main ()
{
    string mystr;
    cout << "Please enter an interger value up to "
    "the length of five numbers: " << endl;
    getline (cin,mystr);
    n0 = mystr[0];
    n1 = mystr[1];
    n2 = mystr[2];
    n3 = mystr[3];
    n4 = mystr[4];
    
if  ({ n4 = &0;
    cout << n3;
    cout << n2;
    cout << n1;
    cout << n0;)
}
if ({ n4 + n3 = &0;
     cout << n2;
     cout << n1;
     cout << n0;)
}
if ({ n4 + n3 + n2 = &0;
     cout << n1;
     cout << n0;)
}
if ({ n4 + n3 + n2 + n1 = &0;
     cout << n0;)
}
else
    cout << n4;
    cout << n3;
    cout << n2;
    cout << n1;
    cout << n0;
    _getch();
    return 0;
}

The error is as follows: In function `int main()':
Line 23 expected primary function before '{' token
Line 23 expected ')' before '{' token

It gives me an error about the if functions, I really want to get them to work in about the same way I have coded, because that is what I am most familiar with. Any help will be appreciated and thanks for your time.

P.S. I didn't have those parenthesis in the first draft I had, It gave me less errors so I assumed it was more correct, please correct me if i'm wrong.

Recommended Answers

All 10 Replies

You need to look up the format of the IF statement. Not one of them is even close to being correct.

Ah, that is my bad. I don't get to use the internet as much as I used to so I tried remembering the IF statement from memory, lol bad idea. Anyways, I put it in the right format now and I am wondering how exactly or what exactly to put in after the = sign in my IF statements... I thought &0 would work, but I guess not. Any suggestions?

#include <iostream>
#include <conio.h>
#include <string>
#include <sstream>
using namespace std;

char n0,n1,n2,n3,n4;
string mystr;

int main ()
{
    string mystr;
    cout << "Please enter an interger value up to "
    "the length of five numbers: " << endl;
    getline (cin,mystr);
    n0 = mystr[0];
    n1 = mystr[1];
    n2 = mystr[2];
    n3 = mystr[3];
    n4 = mystr[4];
    
if  (n4 = &0)
    {cout << n3;
    cout << n2;
    cout << n1;
    cout << n0;
}
if   (n4 + n3 = &0)
     {cout << n2;
     cout << n1;
     cout << n0;
}
if   (n4 + n3 + n2 = &0)
     {cout << n1;
     cout << n0;
}
if   (n4 + n3 + n2 + n1 = &0)
     {cout << n0;
}
else
    cout << n4;
    cout << n3;
    cout << n2;
    cout << n1;
    cout << n0;
    _getch();
    return 0;
}

Thanks for the time. I will probably respond tomorrow since I must get some sleep.

Ah, that is my bad. I don't get to use the internet as much as I used to so I tried remembering the IF statement from memory, lol bad idea. Anyways, I put it in the right format now...

Try again. =? No.

... and I am wondering how exactly or what exactly to put in after the = sign in my IF statements...

What's the purpose of the IF? What do you want to compare your value to? That's what goes after the ==.

if ( x == 0 ) 
{
    //expression;
}

WaltP, I am trying to compare with my IF statement, not the number zero, but the value of zero, which is nothing, so that when I would put in a number less than five digits it would show it correctly and neatly. That is why I had &0 because I thought that it would make it a space, since the value of 0 is nothing. And then I would work off of that and make the numbers go in the right slots nice and neat looking.

Like for example, when I put it 105, it would return (weird symbol)(letter)501, but I would ideally want it to be (space)(space)501. And eventually just 501 with nothing following it or before it.

if ( x == 0 ) 
{
    //expression;
}

That is what I was going to do, but wouldn't that screw up with numbers that are like 1024, it would return 421 or some funky number that is not right.

I just tried it and it did not work as i want it to. 104 returned, (symbol)(space)401 instead of the desired 401(empty)(empty). 1305 return 5031 5031, almost correct, it did it right but it displays it twice.

Anyways, here is what I have so far on that in code form, I got rid of the &0`s I had in there.

#include <iostream>
#include <conio.h>
#include <string>
#include <sstream>
using namespace std;

char n0,n1,n2,n3,n4;
string mystr;

int main ()
{
    string mystr;
    cout << "Please enter an interger value up to "
    "the length of five numbers:" << endl;
    getline (cin,mystr);
    n0 = mystr[0];
    n1 = mystr[1];
    n2 = mystr[2];
    n3 = mystr[3];
    n4 = mystr[4];
    
if  (n4 == 0)
    {cout << n3;
    cout << n2;
    cout << n1;
    cout << n0;
}
if   (n4 + n3 == 0)
     {cout << n2;
     cout << n1;
     cout << n0;
}
if   (n4 + n3 + n2 == 0)
     {cout << n1;
     cout << n0;
}
if   (n4 + n3 + n2 + n1 == 0)
     {cout << n0;
}
if (n4 + n3 + n2 + n1 + n0 == 0)
{ cout << 0;
}
else
    cout << n4;
    cout << n3;
    cout << n2;
    cout << n1;
    cout << n0 << endl;
{
int exit;
while(1 == 1)
           {
           cout << endl;
           cout << "Type 1 to end this program, otherwise type anything"
           " else to continue" << endl;
           cin >> exit;
           
           if(exit == 1)
           { break; }
//           else 
           }
}
return 0;
}

I wanted to add a way for the user to exit or keep going, but I have not found a good way to go back to entering numbers.

WaltP, I am trying to compare with my IF statement, not the number zero, but the value of zero, which is nothing,

What is the difference between the number 0 and the value of zero ? And what is the value of nothing ? None of this makes any sense. Please describe again what you are doing and don't mention zero because it means nothing in the context you are attempting to describe.

That is why I had &0 because I thought that it would make it a space, since the value of 0 is nothing.

No, &0 is the address of the constant zero -- which has a value -- the address.

A SPACE is not 0, it's 32 decimal, 20 hex.

The value of 0 is 0. nothing is meaningless as a value.

Like for example, when I put it 105, it would return (weird symbol)(letter)501, but I would ideally want it to be (space)(space)501. And eventually just 501 with nothing following it or before it.

You are using a character array. So, if the array element is a digit ('0' thru '9') then print it.
If the array element is not a digit, print a SPACE.

What I meant to say was that I was trying to make an IF statement so that it excludes certain array elements, so that 123 would input 321, using as many as it started with and no more or less. I hope I made sense this time.

So my suggestion makes no sense to you? Why not?

Why don't you just get input as a string and go through it and store all the numbers into a new array.

for example:

#include <iostream>
using namespace std;

int main()
{
	string line, flipped = "";
	cout << "Please enter a string of numbers (ie \"12345\"): ";
	getline(cin, line);

	for( int i = line.size()-1; i >= 0; i-- ) //iterate through the string backwards
	{
		if( line[i] >= '0' && line[i] <= '9' ) //pick out all the numbers
			flipped += line[i]; //add them to the end of the new string
	}

	cout << flipped << endl;

	return 0;
}

Looks like you gave solution, but the flipped variable is not necessary when you can just output directly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.