I am learning C++. Sure the book examples and programs are fun, but I want to learn the language. In order to learn it I want to figure out how to write a program I invent.
I want to write a guessing program. Where you will input a string of numbers and then C++ will find the numbers entered and out put them. Sure I could set the output to the input, but that is no fun.
I understand that I will need to use a lot of while loops. The first part of the program I found by getting the number of digits entered. I divide the number by 10 until I hit 0. Then the number of times I divide become the total number of places. Then once I know the number of places, I want the program to go through all the numbers starting at 0 until it is equal to the number that belongs to that place.
The problem I have is how do I write a program that will change the variable name. Where I increment the variable by 1 for each time I do the division.
I started programming with a flowchart program named Raptor. With that program all I need to do is use an array to store the numbers entered and then I can reference the array as it stores the array as the same number in the counter. For example Guess1 is the first time. Then Guess2 is the second time.
Changing variable names as far as I know in the text of the code is not possible, and wouldn't do what you want to do. What you REALLY want is a vector.
There's three solutions for this: the generic solution, where you do the conversion yourself; the C solution, with sprintf(), and the C++ solution, with std::stringstream. I won't show you the first one, but I will show you the other ones, so that you see how this is normally done:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int n=/*...*/;
//C:
char s[/*a reasonably large number. It should be at least log10(INT_MAX)+2*/];
sprintf(s,"%d",n);
for (size_t a=0;s[a];a++)
printf("%c",s[a]);
//C++:
std::stringstream stream;
stream <<n;
std::string s=stream.str();
for (size_t a=0;a<s.size();a++)
std::cout <<s[a];
The C++ solution is preferable because there's no risk of a buffer overflowing. The same can't be said of sprintf().
When I try the code suggested I got a lot of compiler errors.
Here is my total code. I moved around the code you gave to get rid of errors. I started with over 15 and shrunk it to 5 by moving the code around.
#include<iostream>
using namespace std;
std::stringstream stream;
int n;
stream <<n;
struct guess
{
int number;
int primer;
};
int main()
{
std::string s=stream.str();
for (size_t a=0;a<s.size();a++)
std::cout <<s[a];
guess attempt;
int counter;
int Value;
int checker;
counter = 0;
cout << "Please input your number. This program will guess the number.";
cin >> Value;
attempt.number = Value;
while (attempt.number != 0)
{
attempt.number = attempt.number / 10;
counter = counter +1;
cout << "\n" << attempt.number;
}
cout << "\nThe number is: " << Value;
cout << "\nThe number of digits in the number is " << counter;
system("pause");
return 0;
}
You need #include <string> and #include <sstream>.
std::stringstream stream;
Above you want ostringstream.
stream <<n;
This is just hanging out in the global scope. Move this within the main function to wherever you need it. You're also using both usingnamespace std; and std::. Pick one or the other.
There's nothing wrong with it, but in terms of consistency and readability it will most likely benefit him and others reading his code in the future. At least in my opinion it would.