General

Tech

Help with a C program

Ok guys, can you please help me with a C++ program which prompts the user to enter numeric quantities, and allows the user to use the engineering prefixes to specify the power of 10. Store the values entered in a double precision number and display then in scientific notation.
(e.g. the user should be able to enter 345u, and it will be recognised and stored as 345 x 10^(-6))
Prefixes you should be able to process ...
What I've done so far:

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

void getInput(string &SI, double &Calc, string &Number){
cout << "Please enter the number followed by the si prefix." << endl;
cout << "Number: " ;
getline(cin, Number); //Get user input and store in number string
SI = Number[Number.length()-1]; //get the last character of the string and store as SI.
Number.erase(Number.size()-1); //Erase the last character of the string
stringstream(Number) >> Calc; //Safely change the datatype of Number and store the result in calc.
} string siAr[10] = {"T","G","M","k","d","c","m","u","n","p"}; //Prefix Array
double nAr[10] = {1e+12,1e+9,1e+6,1e+3,1e-1,1e-2,1e-3,1e-6,1e-9,1e-12};//Array holding the values of the SI letters.

while (i<10 )//loops for 10 times
{ if(siAr[i]==SI){ //Checks each element of the siAr array against the letter stored in SI, when it finds a match set numSI to i
numSI=i;
} i++;//Increment i
}

Really noobish at this kind of thing, and my lecturer is very unhelpful (he even constantly says 'in C, we do this and this, when in fact we're learning C++). Lecture slides are also unhelpful. Appreciate any help. I'm assuming I have to add an int main part somewhere down.

May 26, 2012

1 Comment • Newest first

Fiercerain

I'd say ask the TA if one is available for your class. Keep debugging.

Reply May 27, 2012