Solved

Using variable to store number and text

Posted on 2004-09-20
15
158 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
Comment Utility
i get possible loss of data in conversion from float to char...
0
 
LVL 10

Expert Comment

by:Sys_Prog
Comment Utility
POst some of your code

amit
0
 

Author Comment

by:KalluMama
Comment Utility
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
 
LVL 10

Expert Comment

by:Sys_Prog
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Or what u can do is always store in string type of variale since u do not need calclations

Amit
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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 200 total points
Comment Utility
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 200 total points
Comment Utility
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
Comment Utility
Correction:

    cout <<  "Rejected" endl;  

turns to

   cout <<  "Rejected" << endl;

Regards, Alex


0
 

Author Comment

by:KalluMama
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now