Some help

Hi guys!

So I'm a beginner at C++. I've been playing around with it, since I wanna learn more and I found this site, so I will be asking some question here and there:)

My current problem is this. I want the program to write all the 3 digit numbers (meaning from 100 to 999), but I want them to be 10 per row. So far I've been able to get them to a 1 per row, which goes like this

100
101
102
..

And this is the code I got.

{
int i;

for (i=100;i<1000;i++)
cout<<i<<endl;
}

-----------------------------------------------------------------------------------------

Another problem I thought of. If I want the program to display "1" if a 4 digit number, lets say 5665, is symmetrical. How would that look like in c++? I can think of 3 ways (theoretically) that could be done, but I'm not sure how to write it.
Last edited on
add if(i%10 == 0) cout << endl; in the for loop and change existing cout to cout << i << ' ';
Last edited on
@codewalker

When I add this, it displays 20 numbers per row.

100 110 120 130 140 150 160 170 180 190 200... 290

I want them to be 10 per row.
can you post updated code please?
And in code tags too. Ta.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main ()
{
    int i;

    for (i=100;i<1000;i++)
    if(i%10==0)
    cout << i <<' ';
}


Here's an image.

http://i.imgur.com/nxXwhno.png
Last edited on
braces braces braces!

1
2
3
4
5
6
for (i=100;i<1000;i++)
{
// stick everything that should go in the for loop between braces
// it's also good habit to to this for if's as well, even if you've
// only got one line under it.
}


edit:
You have also pretty much ignored codewalker's advice.
Last edited on
Alright, it worked! Thank you.

What about my other question?

Another problem I thought of. If I want the program to display "1" for true and 0 for false, if a 4 digit number, lets say 5665, is symmetrical. How would that look like in c++? I can think of 3 ways (theoretically) that could be done, but I'm not sure how to write it.
Last edited on
You could convert it to a string


1
2
3
    int myInt = 5665;
    std::string myString = std::to_string(myInt);
 


, and then do something like:

1
2
3
4
5
bool match = false;
if( (myString[0] == myString[3]) && (myString[1] == myString[2]) )
{
    match = true;
}


This assumes you only care about 4 digit numbers.
Last edited on
Could you write the whole code please? I tried what you suggested and the program found like 7 errors in what I wrote.
no i won't.
show updated code and errors.
Alright, I don't know what I did but it seems to work right now.

You guys were very helpful, thank you. I will probably have more questions in the future, so I won't hesitate to ask.
Topic archived. No new replies allowed.