Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using variable to store number and text

Posted on 2004-09-20
15
Medium Priority
?
169 Views
Last Modified: 2010-04-01
Is it possible to store float or text in a variable (depending on certain criteria). I am trying to do this...if criteria A is selected the display a float value else display a text "The End"...something like this...
0
Comment
Question by:KalluMama
  • 5
  • 5
  • 3
  • +2
15 Comments
 

Author Comment

by:KalluMama
ID: 12107854
i get possible loss of data in conversion from float to char...
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 12107870
POst some of your code

amit
0
 

Author Comment

by:KalluMama
ID: 12107899
if (wgt <= 50 && largestDim <=36 && maxSize < 72)
{
//The criteria was met, so find out the cost
wgtIndex = wt_index(aInt, ARR_MAX, wgt);
cost = aFloat[wgtIndex];
}
else
{
//The criteria was not met so display, rejected
cost = "Rejected";
}

cout << cost;
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 10

Expert Comment

by:Sys_Prog
ID: 12107963
What is type of cost...

U cant have cost have string as well as float

however, u could use uniion for that

Amit
0
 

Author Comment

by:KalluMama
ID: 12107996
i need to show output in one column. If certain criteria matches, then display the cost, else display rejected. Any ideas how to do this...there will be no calculations performed on these numbers.
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 12108271
displayng is not a problem
the problem is that u are using the same variable to store the output...thats not possible

Amit
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 12108288
Or what u can do is always store in string type of variale since u do not need calclations

Amit
0
 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 400 total points
ID: 12108328
Try tis

#include <iostream>
#include <sstream>


using namespace std ;


main() {

      stringstream      str ;

      int f = 10 ;

      for ( int i = 0 ; i <= 1 ; i++ ) {
            if ( i == 0 ) {
                  str << "hello" ;
            }
            else {
                  str <<       f ;
            }
            cout << endl << str.str() ;
      }


}

Amit
0
 

Author Comment

by:KalluMama
ID: 12108837
is there a way to do this without using stringstream? i have not been taught that so i cant use it... :(


0
 
LVL 1

Expert Comment

by:decentswati
ID: 12109378
hi kallumama,
if u know void pointer then try to achieve that with its help.
u can decide at runtime what type of value u want to display,without loosing any data.

try it may sure help u..
-decentswati
0
 
LVL 9

Accepted Solution

by:
jhshukla earned 800 total points
ID: 12109573
typedef struct Cost{
  bool accepted;
  float value;
} Cost;

Cost cost;

if(criteria met){
  cost.accepted=true;
  cost.value = somefloat;
}
else{
  cost.accepted = false;
  cost.value = -1; //or whatever errorcode you want
}

cout << (cost.accepted ? cost.value : "rejected");

but i would prefer union as amit suggested.

and if you are sure that acceptable cost is >=0 then instead of cost = "rejected" you could say cost = -1 and then
cout << (cost < 0 ? "Rejected" : cost);
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 800 total points
ID: 12111273
Or simply omit cost variable and have different output:

if (wgt <= 50 && largestDim <=36 && maxSize < 72)
{
//The criteria was met, so find out the cost
wgtIndex = wt_index(aInt, ARR_MAX, wgt);
   cout <<  aFloat[wgtIndex] << endl;
}
else
{
//The criteria was not met so display, rejected
   cout <<  "Rejected" endl;
}

If you have to use cost variable, choose a char array (or better a string type, but you may not be allowed to use string?).

char cost[256] = "";  // make it big enough for any reasonable input
if (wgt <= 50 && largestDim <=36 && maxSize < 72)
{
   //The criteria was met, so find out the cost
   wgtIndex = wt_index(aInt, ARR_MAX, wgt);
   gcvt(aFloat[wgtIndex], 6, cost);  // 6 is the # of significant digits
}
else
{
   //The criteria was not met so display, rejected
   strcpy (cost, "Rejected");
}

cout << cost << endl;

Regards, Alex

p.s. tell me, if you need a solution using string type


0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12111282
Correction:

    cout <<  "Rejected" endl;  

turns to

   cout <<  "Rejected" << endl;

Regards, Alex


0
 

Author Comment

by:KalluMama
ID: 12112053
Alex,

Since we have not been taught 'gcvt', it will be odd for me to use it. Since i am doing no calculations...could i only use string. There are many outputs to follow and this will be one of them which will be displayed in the program later depending on criteria earlier...
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12123473
Sorry, i replied to your last comment but forgot to submit somehow. Now it is lost... but i'll try again:

My first sample code doesn't use gcvt. Isn't it an alternative?

>>>> Is it possible to store float or text in a variable

Your original question can be answered with yes. That variable could be a char array or a string type. But, you have to convert float to string then. For that i know 4 ways:

1. gcvt (or ecvt, fcvt)
2. stringstream
3. sprintf
4. an own conversion function

You are not allowed to use (1) or (2), right? With sprintf the conversion is like that:

      char cost[256] = "";
      sprintf(cost, "%f", aFloat[wgtIndex]);

If (4) is an alternative, tell me and i will help you with that.

If you could use none of these, your initial question must be answered with: "Yes, but not with the means you have been taught til now".

Regards, Alex
 


 
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question