d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ Returning A String > String Is Empty After I Return It?
12Next
Add Reply New Topic New Poll
Member
Posts: 625
Joined: Nov 18 2009
Gold: 23.95
Mar 25 2013 04:35pm
First question is why doesn't the string show after returning it from testingstr().
Second question is if I was passing it to another function, Is it done correctly below or what am I missing when passing it?
Thanks!
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Main Driver File

#include "testing.h"
#include <iostream>

using namespace std;

string teststr;

int main()
{

testingstr();

cout << endl << endl << endl << endl << endl;
cout << "Testing String in Main after returning: " <<teststr <<endl; <---------------------- Doesn't show here.

randomfunc(teststr);

return 0;

}

--------------------------------------------------------------------------------------------------------------------------------
Header file

#include <string>

using namespace std;

string testingstr();
void randomfunc(string teststr);

---------------------------------------------------------------------
Function file

#include "testing.h"
#include <iostream>
#include <string>

using namespace std;


string testingstr()
{
string teststr;

cout << "Please enter phrase: ";
getline(cin, teststr);

cout << endl << endl;
cout << "Testing String in Function before returning: " <<teststr<<endl; <--------------------- Works here though.

return teststr;
}


void randomfunc(string teststr)
{
cout << "Testing string if passed to another function: " <<teststr<<endl;
}


Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Mar 25 2013 04:55pm
the return value of a function is usually saved in the EAX register. ONE register... thats usually the size of an integer, or a pointer, but a whole string is too much for a return value. so whats "return teststr;" supposed to do? also if you quit the function, the local variables aren't valid anymore.

if you want to return a string, learn to use pointers. or already make an output to the console inside of the function

edit: -------------------------------------------------------
edit3: i take back the stuff i said the line above... cuz multiple files it's not overriding anything

edit2: your problem is a "c" problem, though u use "c++" functions and a "c++" compiler. at least I can't see stuff like classes and methods


This post was edited by Richter on Mar 25 2013 05:03pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 25 2013 05:00pm
Quote (Richter @ Mar 25 2013 06:55pm)
the return value of a function is usually saved in the EAX register. ONE register... thats usually the size of an integer, or a pointer, but a whole string is too much for a return value. so whats "return teststr;" supposed to do? also if you quit the function, the local variables aren't valid anymore.

if you want to return a string, learn to use pointers. or already make an output to the console inside of the function

edit: beside that, you got 2 variables called teststr... one is global and there is also a local one, overriding the global one (at least overriding inside the function).


this.

you either have to make a constant string inside the function and return that as a pointer. or pass a pointer to a string variable so your function can move your string to that address. eitehr way you need to use pointers

This post was edited by AbDuCt on Mar 25 2013 05:01pm
Member
Posts: 625
Joined: Nov 18 2009
Gold: 23.95
Mar 25 2013 05:06pm
Im changing my code for that, my teacher gave us the function prototypes and the string function wasn't passed anything.
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Mar 25 2013 05:13pm
maybe call the function "testingstr();" later, so that you can make a "cout" inside the function? would be easiest...

Code
cout << endl << endl << endl << endl << endl;
cout << "Testing String in Main after returning: ";
testingstr();
cout << endl;


edit: in your version, you also don't do anything with the result of "testingstr();". so even if you return anything, it won't be used.

This post was edited by Richter on Mar 25 2013 05:15pm
Member
Posts: 625
Joined: Nov 18 2009
Gold: 23.95
Mar 25 2013 05:47pm
I got it to work and successfully passed it and successfully was able to take each char in the string in my program and report if the user inputted special characters, a space, or numbers, now i just need to take each character in the string and assign it as a integer.
The program is taking a 8 letter phrase and using a calling pad assign a number for the letter to make a safe combination.
Any idea how to take out a a single character in the phrase and assign it a number ( a, b , c would be 2 & w, x, y, z would be 9 and etc.) so thedoggy is 84 - 99 - 64 - 43 and then take each of those integers and compare them to each other and if any is =< 4.0 of each other.

Also thanks for the help too!
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Mar 25 2013 07:35pm
well, the characters in a string are for the computer already a number.
you need to look at the ascii-table (easily found on google) which character got which number.
if you only want to have lowercase charaters, you don't need to convert uppercase characters to lowercase charaters. (but if you need help at that too, just ask)
a b c d e f g.... are the numbers from 97 upwards
so, if you take the number of a character and subtract 97 you have another assignment: a=0, b=1, c=2... etc.
now you could make an array which stores the assignments like this:
numbers[26] = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9,9}
now if you want to get the number of 'd', you first subtract 97, which gives you '3', thats the forth position in the array, which is in this case a '3'.
what i was talking about looks like this: numbers[characterToTest - 97]
but first need to make sure you don't access numbers which are lower than 97 or higher than the value of 'z', because then you access something outside of your assignment-list.

was just an idea how you could implement it, or at least what i understood you wanted to do ;)
Member
Posts: 625
Joined: Nov 18 2009
Gold: 23.95
Mar 25 2013 07:57pm
I looked at the table, curious question: is it possible to make a valid code by using the #'s that represent the letter?
So like

Example:

int main()
Dec

105,110,116, 32 ,109, 97,105,110,40,41
i , n , t ,space, m , a , i , n , ( , )
----------------------------------------------------------------------------------------------------------------------------------------------------------
Back to program haha, well this is the concept I'm trying to make in my function:
http://images.mobilepatterns.com/wordpress/wp-content/uploads/apple-phone-dialer.png

A B C && a b c == 2
D E F && d e f == 2
G H I && g h i == 4
etc...

Heres the description of what the function:
"This function is passed two character values, and it combines them into an integer. For example, if 'J' and 'o' are passed it, it returns a 56. NOTE: you may assume only a-z and A-Z are passed to this function.
int FuncCharToInt(char a, char b)

I have this for taking each character from a string and returning it to the main for it to be passed into int FuncCharToInt(char a, char b)
Code
for(int i = 0; i < pI.at(i); i++)
{
 pI.at(i) = a;
 return a;                            //I have the variables delcared at the top of the function.
 pI.at(i+1) = b;
 return b;
}


My problem now is passing those to the next function and then assigned it an integer.
So far i have this:
Code
int twoCharsToInt(char a, char b)
{

for(int i = 0; i < a; i++)
{
 if(a = 'a' || a == 'b' || a == 'c' ||                                 //Im getting an red mark underneath each letter saying: "expression must be a modifiable value"
   a = 'A' || a == 'B' || a == 'C')
  {
   return 2;
  }
 
  else if(a == 'd' || a == 'e' || a == 'f' ||
    a == 'D' || a == 'E' || a == 'F')
  {
   return 3;
  }

  else if(a == 'g' || a == 'h' || a == 'i' ||
    a == 'G' || a == 'H' || a == 'I')
  {
   return 4;
  }

  else if(a == 'j' || a == 'k' || a == 'l' ||
    a == 'J' || a == 'K' || a == 'L')
  {
   return 5;
  }


}


How do I modify one of those expressions so it will return char a and b if both happen to be a A or b B c C etc. since im passing two characters into the function.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 25 2013 08:38pm
why not just use a toupper() function before parsing your characters
Member
Posts: 625
Joined: Nov 18 2009
Gold: 23.95
Mar 25 2013 08:40pm
haven't learned that one yet.

I managed to fix the issue and not just copying each if statements again for each letter and number haha.
How could i take the char a and b and put them pack into a string? but their gonna be numbers.
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll