### How do I get the last number isolated out of a count variable?

I've been working on this program to get input letters to repeat certain amounts of ones and I want to get amounts of one repeats to add up. I'm not exactly getting an error, I just want to figure out how to go from here to get these one repeats into added up numbers. I've been working on a phase where I'm concentrating on the first output variables so that I can know what to do with the rest of them. Here is the code:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174`` ``````#include #include #include #include using namespace std; int main() { int done = 0; int flag; int count = 1; char word, letter; unsigned long long xone; int xtwo; int xthree; int xfour; int xfive; int xsix; int xseven; int xeight; int xnine; int xzten; int n_one=0; int ntwo=0; int nthree=0; int nfour=0; int nfive=0; int nsix=0; int nseven=0; int neight=0; int nnine=0; int nzten=0; while (done != 1) { flag = 0; count = 1; while (flag == 0) { cin >> word; word = toupper(word); switch (word) { case 'A': xone = 1; break; case 'B': xtwo = 1; break; case 'C': xthree = 1; break; case 'D': xfour = 1; break; case 'E': xfive = 1; break; case 'F': xeight = 1; xsix = 1; break; case 'G': xthree = 1; xseven = 1; break; case 'H': xfive = 1; xeight = 1; break; case 'I': xone = 1; xnine = 1; break; case 'J': xone = 1; xsix = 1; xzten = 1; break; case 'K': xtwo = 1; xone = 1; xzten = 1; break; case 'L': xthree = 1; xtwo = 1; xzten = 1; break; case 'M': xthree = 1; xzten = 1; break; case 'N': xfive = 1; xfour = 1; xzten = 1; break; case 'O': xseven = 1; xfive = 1; xzten = 1; break; case 'P': xsix = 1; xzten = 1; break; case 'Q': xone = 1; xseven = 1; xzten = 1; break; case 'R': xtwo = 1; xeight = 1; xzten = 1; break; case 'S': xthree = 1; xnine = 1; xzten = 1; break; case 'T': xfour = 1; xone = 1; xzten = 1; break; case 'U': xsix = 1; xtwo = 1; xzten = 1; break; case 'V': xsix = 1; xseven = 1; xzten = 1; break; case 'W': xsix = 1; xzten = 1; break; case 'X': xfive = 1; xzten = 1; break; case 'Y': xone = 1; xfour = 1; xzten = 1; break; case 'Z': xseven = 1; break; } count++; while(xone) { ++n_one; xone /= 10; } cout << n_one << endl; } } done = 0; return 0; }``````

This is what the program outputs when I type a bunch of "a"'s:

Input(cin):
 ``` aaaaaaaaaaaaaaaaaaaaaaa ```

Output(cout):
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ```

I want to get that last number into a variable. Not just that specific number, but I want to get the last number of any amount that happens. If someone could help me out with this, it would be greatly appreciated. Just focus on what is going on with the xone and n_one variables. Once this issue with that variable can be figured out, the rest of them will be.
I am struggling to understand the whole story of what you are trying to achieve and your method of doing it.

Is there a reason why you cannot just put the length of the input string into a variable?

I guess your situation is more complex than that.

Is the input mixed up a bit like this:

 `aaaaaaabbbbbbbbbcccccfffffffzzzzzzzggggggoooooo`

If so there are easier ways to count repeated chars.
I want the length of xone to be however many times word causes to cases to cause xone to equal 1. I want to know how many times one repeats in xone. I want to use the amount of repeats as multipliers. I want the amount of ones that xone repeats, to be put into its own variable. Once I can actually do that, I can do that to the rest of the variables. When I get how many ones that xtwo repeats, I'm going to use that amount to multiply by 2. There will also be xthree repeats to multiply by 3 and so on. That is what I want to do.
 http://www.cplusplus.com/reference/algorithm/count/

You can use the count algorithm with strings as well.

So you could use this to count the number of occurrences of a string within another string.

You have given us your algorithm (method) of your program ( I am still not entirely clear on this) - just wondering what you are using the program to do? That is, what what is the real world use for your program? This might help us all suggest a good solution for you.

 to cause xone to equal 1.

Are you using the value of 1 to mean true instead of a bool variable?

HTH
1 is being used as a repeater to determine numbers to multiply by in that variable. This is a kind of numerology program that I am working on. I have some ideas on how to use it for things like arranging colors and notes certain ways for meditations for example. I also want to build it into a video game. I want to do whatever I can think of doing with it. I think that I'm going to have to rearrange things with the input and output because of this series of while loops within while loops. I actually just figured out writing that last line into a text file. I figure that I should do some file linking, because of this repeat issue. I gotta keep this translator-repeater code in its own main in its own file, then link another cpp file to it with an h file. In the other cpp file, I should do a cin input with a char that I should add to the word char. In the same file, read the text files. Because the output number gets written into the text file that is being read, hopefully there wont be a linking issue. That would be another thing to deal with.
I am not getting it........ Probably because I don't understand anything about numerology.

In your original example, you had a string of 23 'a' characters as input. The output was the numbers 1 to 23.
You wanted to store the value 23 into a variable.

I don't see why you need 170 lines of code to do this.

The count algorithm can count the number of occurrences of the char 'a' in the input string.

 I want to know how many times one repeats in xone.

This is counting the number of one's in xone isn't it? Maybe you could come up with better variable names?
I figured out how to get that. Now, I have to somehow get around the looping structure to make things not repeat like they do. I made it so that the amount of one repeats is stored in a text file. Now all I have to do is be able to read the text file and output its contents outside of the while loop structuring that makes things repeat.
Can there be a way to redo the while loop structuring?
I am still confused / ignorant of this whole thing.

Can you post a detailed and real example of what the input looks like, and what you would like the output to be?
I want the first basic input and output screen to look like this, pretty much:
 ``` Number Tree System Enter a Name or a Word: Quantum 170.620.1.540.410.620.430 1 x 3 = 3; 7; 10 x 6 = 60 = 6; 6 x 2 = 12 = 3; 2 x 2 = 4; 5; 4 x 3 = 12 = 3; 6; 3; ```
I think the best thing to do here is to design your code by writing the methodology as comments. This can be done iteratively - start with general ideas, then go back and keep refining it, until you reach the point where you are happy to write code. Leave the comments in, as they serve as documentation.

This will help to organise things logically, identify functions, which control- flow methods (if, loops etc) to use, data structures and types to store info. Hopefully this will result in elegant and efficient code.

To start off, I came up with this:

 ``123456`` ``````//Convert characters in a word into number codes //details of how to do this //count occurrences of each digit in the number string - use count algorithm //need to keep track of digits already counted and their position in the string //mutilply count by digit = sum //digital root function to add digits in sum ``````

So have a go at that, see how you get on.
Right. The exact issue that I know that I need to get passed, is a looping and output issue. I got the number that I need to write into a text file. NOW, all I need to do is find the right way to get that number from the text file into a variable in the same file. Once I can get that done right, I know what I need to do from there. I've had most of the general math methods planned out while also knowing that I had to work out some issues along the way. Functional issues. I recently included cmath and after the count++ I replaced that previous code with this code instead:
 ``12345678`` ``````double rep1; rep1 = ceil(log10(fabs(xone)+1)); ++n_one; ofstream myfile; myfile.open ("xone.txt"); myfile << n_one; myfile.close();``````

Those alterations successfully write to xone.txt when you have that file in an include directory in the compiler settings. Now that that works, I want to figure out how to read from the text file and work with the number, then work with all the variables like that. This is the code that I plan on using somewhere to read from the text file:
 ``1234`` ``````std::ifstream input("xone.txt"); int x1; input >> x1; cout << x1;``````

All I gotta do is figure out where and how to execute that code to read from the text file correctly and work with the number correctly without the loops doing things to it that are against the objectives.
But why do that?

I don't see the point in writing the value of a variable to a file, only to read it in again into another variable.

Why not just assign it to the new variable?

 All I gotta do is figure out where and how to execute that code to read from the text file correctly and work with the number correctly without the loops doing things to it that are against the objectives.

I hope you are not trolling by being overly pedantic. I have made a number of posts and we don't seem to be getting very far.
The point of transferring through a file is getting the amount values for multipliers in this loop . It is the only way I could do it. I tried what you just suggested to me and it did not work, because of the loop. I can get the numbers I want through to a display now. I ran into a new problem so far. I'm using multiple counts and they are interfering with each other.
I took this project a whole other direction with strings. Now all I gotta do is figure out how to allocate enough memory to make it so the exe doesn't crash.
Topic archived. No new replies allowed.