Pointers
-
Hallo Leute,
ich würde gerne in mein selbstgeschriebenen Code "Pointers" einfügen. Könnt Ihr mir sagen wo ich diese am besten einfügen kann.
#include <iostream> #include <iomanip>// Allows user to set precision #include <string> using namespace std; struct conversion //Groups conversion information { double Dollar; double rate; string totalCalc; string description; } currency[5]; void initVariables(); void displayMenu(); void displayResults(); int main() { bool hasInputFailed; char cont; bool docont = true; do { //Initialize structures and arrays initVariables(); system("CLS"); //clears the screen displayMenu(); displayResults(); do //Testing character input { hasInputFailed = false; cout << "\n\nWould you like to continue? (y/n)\n"; cin >> cont; if ( (tolower(cont) != 'n'&& tolower(cont) != 'y') || cin.fail() ) { cin.clear(); cin.ignore(100,'\n'); cout << "Invalid response, try again:\n"; hasInputFailed = true; } if (tolower(cont) == 'n') { docont = false; } else { docont = true; } }while ( hasInputFailed == true); } while (docont == true); return 0; }//End of main void initVariables() { currency[0].description = " Euros = 0.762953 ";//Display of International rates to a US Dollar currency[0].rate = 0.762953; //From http://www.xe.com/ucc/full.shtml currency[0].totalCalc = "Euros"; //For total calculation display currency[0].Dollar = 0.0; currency[1].description = " Swiss Francs = 1.17806 "; currency[1].rate = 1.17806; currency[1].totalCalc = "Swiss Francs"; currency[1].Dollar = 0.0; currency[2].description = " British Pounds = 0.534387 "; currency[2].rate = 0.534387; currency[2].totalCalc = "British Pounds"; currency[2].Dollar = 0.0; currency[3].description = " Japanese Yens = 101.882 "; currency[3].rate = 101.882; currency[3].totalCalc = "Japanese Yens"; currency[3].Dollar = 0.0; currency[4].description = " Russian Rubles = 27.9400 "; currency[4].rate = 27.9400; currency[4].totalCalc = "Russian Rubles"; currency[4].Dollar = 0.0; } void displayMenu() { int question; double amount; bool alreadySelected; bool currencySelected[5]; //Keeps track of selections for (int i=0; i < 5; i++) { currencySelected[i] = false; } do { system("CLS"); //clears the screen /* Title of Program */ cout << "\t\t***************************\n"; cout << "\t\tCurrency Conversion Program\n"; cout << "\t\t***************************\n\n"; cout << " International rates to 1 US Dollar as of January 17, 2005\n\n"; cout << "Please choose a foreign currency (1-5)" << endl;//Prompt user for selection cout << "or press 0 when done or wishing to exit:" << endl << endl; for (int i=0; i < 5; i++) { cout << "\t" << i+1 << ". " << currency[i].description << endl; } cout << "\t0. Done/Exit" << endl; cin >> question; //Reads user's input //check for errors if (cin.fail() || question < 0 || question > 5) { cin.clear();//Reseting input to allow new input cin.ignore(100,'\n');//Removes bad data up to 100 characters cout << "\nYou entered an invalid selection, please try again." << endl; continue; //back to the beginning of the loop } else if (question == 0) { break; } //check if currency has been already selected, if not select it alreadySelected = false; for (i=0; i < 5; i++) { if (question - 1 == i) { if ( currencySelected[i] ) // if it was already selected { cout << "\nThis type of currency has been selected already" << endl; alreadySelected = true; } else //was not selected before { currencySelected[i] = true; } } } if (alreadySelected == true) { continue; } //Prompt user for Dollar amount(s) cout << "\n\t\tPlease enter amount of US Dollars to convert:" << endl; cin >> amount;//Reads user's input //check for errors while ( cin.fail() || amount <= 0.0 ) { cin.clear();//Reseting input to allow new input cin.ignore(100,'\n');//Removes bad data up to 100 characters cout << "Your entry is invalid, please try again:" << endl; cin >> amount; } //Setting the structure currency[question - 1].Dollar = amount; } while (question != 0); } void displayResults() //Displaying results for each currency and amount entered { for (int i=0; i < 5; i++) { if(currency[i].Dollar > 0.0) { cout << setprecision(2) << fixed; cout << "\n\t" << currency[i].Dollar << " Dollars will buy " << currency[i].rate*currency[i].Dollar << " " << currency[i].totalCalc <<"\n"; } } }Vielen Dank im vor raus
xHITMANx
-
Du weisst schon, was pointer (zeiger) sind, oder?
int i = 2; int* pi = &i; //Der Zeiger pi zeig auf einen Integer (hier i)Du solltest dir das noch mal überlegen und dann kannst du selber entscheiden, wann du einen Zeiger brauchst und wann nicht.
btw:
Man schreibt voraus nur mit einem 'r' und zusammen..