By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

Problem With Integers and Strings

compman9902
100+
P: 105
First of all, thank you in advance for your help and time.
As for my problem, what it is is this:
I have an integer and a string (I'll create a sample program below) and what I need to do is a number is added on to a string ever time it goes around a loop. Every time that number corrisponds with another number in the loop, a letter is added to a seaperate string and every time that happens, the string is reset to equal notheing. Idealy. I will give an example below. If you decide to give me examples, please use the variables that I use, because I'm new at this and it get very confusing some times. Thanks.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <sstream>
  6. #include <string.h>
  7. #include <fstream>
  8. using namespace std;
  9. int main()
  10. //Variables
  11. int position;
  12. string original;
  13. string letter;
  14. string temp;
  15. int a;
  16. int b;
  17. string string;
  18. //Variables
  19. //Declarations
  20. position = 0;
  21. original = "0123456789";
  22. //value of letter is decided below
  23. //value of temp is decided below
  24. a = 01234;
  25. b = 56789;
  26. //value of string is decided below
  27. //Declarations
  28. while (position != length)
  29. {
  30. letter = original[position];
  31. if (temp == a)
  32. {
  33. string = string + "a";
  34. temp = "";
  35. }
  36. else if (temp == b)
  37. {
  38. string = string + "b";
  39. temp = "";  
  40. }
  41. position = position + 1;
  42. }
  43. system("PAUSE");
  44. return 0;
  45. }
  46.  
Please remember that that is only a snippet of my code.
Also:
Operating System:Windows XP (SP2)
C++ Compiler : DEV-C++--4.9.9.2
Mar 22 '07 #1
Share this Question
Share on Google+
5 Replies


Roonie
P: 99
what, exactly, is the problem youre having?

i mean, code snippets: great.
descriptions: better.
description of exactly where you need help: crucial.

(its possible you removed just a little too much information when you gave just a portion of your code.)
Mar 22 '07 #2

compman9902
100+
P: 105
what, exactly, is the problem youre having?

i mean, code snippets: great.
descriptions: better.
description of exactly where you need help: crucial.

(its possible you removed just a little too much information when you gave just a portion of your code.)
I said what I was trying to do...In a way.
Well..I at least implied it.
What i want to do is take a long string of nubers and convert them into their corrisponding character on this side. The rest of the information on my first post should cover the rest of the gaps, but that was basically an overview.
Mar 22 '07 #3

compman9902
100+
P: 105
I said what I was trying to do...In a way.
Well..I at least implied it.
What i want to do is take a long string of nubers and convert them into their corrisponding character on this side. The rest of the information on my first post should cover the rest of the gaps, but that was basically an overview.
and about what i needed help on: right.
well, When I try the code shown below, it dosn't work because of that I'm trying to transfer intigers and strings together. Try running the code.
Mar 22 '07 #4

Roonie
P: 99
and whenever you use a string, you need to have a name for the string instead of using the type identifier "string".

for example:
Expand|Select|Wrap|Line Numbers
  1. if (temp == a)
  2. {
  3. string = string + "a"; //this will give an error because you cant declare a string with the name = string + "a"
  4. temp = "";
  5. }
otherwise, string concatenation should work like you think it does: as long as one of the operands is a string, the other can be char or char[] and the result is a string of the two operands concatenated.

you can add a number (character version) to the string . . . if you need to cast the string to a number, thats a slightly different solution.
Mar 22 '07 #5

compman9902
100+
P: 105
and whenever you use a string, you need to have a name for the string instead of using the type identifier "string".

for example:
Expand|Select|Wrap|Line Numbers
  1. if (temp == a)
  2. {
  3. string = string + "a"; //this will give an error because you cant declare a string with the name = string + "a"
  4. temp = "";
  5. }
otherwise, string concatenation should work like you think it does: as long as one of the operands is a string, the other can be char or char[] and the result is a string of the two operands concatenated.

you can add a number (character version) to the string . . . if you need to cast the string to a number, thats a slightly different solution.
The code Down Their Was Just An Example, but thank you for that knolage, it may come in handy. But the issue has been resolved. Thanks to everyone, without this forum, I don't know what I'd do ;)
Mar 23 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.