C++ help please

JACK@SS

Limp Gawd
Joined
Feb 25, 2001
Messages
388
How can I make a character be recognizes as a value? for example;

I'm asked for an input, I enter "3k", how can I make that "k" be recognized as the value (*1000)? I know how to make the k be recognized as a character, but I can figure out how to give it a value upon input.

Yes this is for homework, but I think that what I want to do is beyond what we are studying, so if somebody could just point to to what I should be looking for, Ill be happy to just learn how to do it, I'm just not sure what to look for.

Thanks.
 
two options

1) tell the user to stop being a tool and just enter 3000

2) you'll have to have the input come in as a string and parse it. Have fun.

When you realize the difficulty and annoyances of (2) you'll realize (1) is a vastly superior route.

or

3) ask for two inputs, the number, then a suffix, such as K, M, etc, and use a switch() to decide what to do from there.

JACK@SS said:
How can I make a character be recognizes as a value? for example;

I'm asked for an input, I enter "3k", how can I make that "k" be recognized as the value (*1000)? I know how to make the k be recognized as a character, but I can figure out how to give it a value upon input.

Yes this is for homework, but I think that what I want to do is beyond what we are studying, so if somebody could just point to to what I should be looking for, Ill be happy to just learn how to do it, I'm just not sure what to look for.

Thanks.
 
I think this is what you are looking for.
I'm not really a C++ programmer but if I understand you, this will do the trick.
This program will take the user's input like 3k and returns it multiplied out.
If the user enters 3k, the program will output 3000, and etc.

hope this helps

#include <iostream>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main(int argc, char *argv[])
{
cout << "Please enter a value: ";

//Take in the users data such as 3k
string userInput;
cin >> userInput;
int positionOfK;
positionOfK = userInput.find('k');//Finds the position of k starting with 0

//Now take off the number part of the string before the k (like the 3 in 3k)
string theRest= userInput.substr(0, positionOfK);
//Convert that to a number instead of it being a string
long int aNumber = strtol(theRest.c_str(),NULL,0);

//Now take aNumber, which is the stuff in front of k and multiply by one thousand
cout << "The number is " << aNumber*1000 <<endl;

/*On my computer I need to stop the window
from closing really quickly so I add this */
system("PAUSE");
return 0;
}
 
Oh yeah, if that helps, plese give me a private message or something so I know.

Hope it helps,
LampShade
 
You can also do it this way.

Code:
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() {
    cout << "Please enter a value: ";
    string input;
    getline(cin,input);
    stringstream ss(input);
    int value;
    ss >> value;
    if (input.find("k") != string::npos || input.find("K") != string::npos) {
        value *= 1000;
    }
    cout << value << endl;
}

You can use float value; or double value; instead if you need to do 3.25K etc.
 
Thank you guys for the help, I will try them out tonight and see what works when I get home from school.

I understand that it might be difficult, I just want to try it.

my assignment was to write a program that includes a long list of stuff. so I wrote a program that does 2 things, it's an Ohms law tool, and it calculates Parallel Resistance. The program is finished and works just fine. The reason I would like to do what I'm asking is that while testing the parallel resistance tool, I realized that most people think of resistance like this; A 2.7k resistor, or 3.4M of resistance. For all intents and purposes my project is done, it works and does everything my instructor wanted, but personally I am annoyed and having to enter 2700, or even worse 34000000, so I've been trying to make it more like something I would want to use.
 
Yes, I also refer to a 3000ohm resistor as a 3K resistor etc. Good idea that you want to do that. :cool:
 
It's harder to do on the command line, but graphically you could take it in 2 fields with the value field being the number, and the unit field being &#937;, K&#937;, M&#937;, etc.…
 
Shadow2531 & LampShade,

For some reason I keep getting this error with both of them.

Fatal Error C1010 unexpected end of file while looking for precompiled header directive


I compiling this with MS Visual C++. (Visual Studio.net)

I have been reading a bit, and I see what both of you are doing, so I see what I can do.
 
start with an empty project instead of a console application. then add a new source file to it. or take the option for precompiled headers out of the projects settings in the compiler tab.

JACK@SS said:
Shadow2531 & LampShade,

For some reason I keep getting this error with both of them.

Fatal Error C1010 unexpected end of file while looking for precompiled header directive


I compiling this with MS Visual C++. (Visual Studio.net)

I have been reading a bit, and I see what both of you are doing, so I see what I can do.
 
Do you have icon similar to a Visual C++ Toolkit 2003 Command Prompt icon somewhere? Studio.net should have it, if the toolkit has it, but ??

If so, just save the program as ohms.cpp, open the c++ command prompt, change to the directory of ohms.cpp and type

cl /EHsc ohms.cpp -o ohms
 
Since its an ohm's law tool, I think something else should be pointed out. A very common notation for resistance is 3K2, which would mean a 3200 ohm resistor. I don't think any of the earlier programs will actually account for this.

Edit: I guess I could have included a solution too :)

In the example that Shadow posted, a few modifications should deal with this. First, scan the string for the "K" before extracting the value from it. If there is a K, keep track of the multiplier, and if you haven't seen a "." yet, change the K into a ".". Extract the value into a type that preserves decimal places (float / double), and then multiply by the multiplier.
 
Back
Top