default

This is a discussion on default within the C++ Programming forums, part of the General Programming Boards category; is it possible to accept the user pressing return when prompted to enter a value??? e.g. cout<<"enter the pressure <100>"<<endl; ...

  1. #1
    newby
    Guest

    default

    is it possible to accept the user pressing return when prompted
    to enter a value???
    e.g. cout<<"enter the pressure <100>"<<endl;
    using this, the user needs only press return to accept the value of 100


    /************************************************** ******************

    THIS IS MY PRESSURE CONTROL PROGRAM
    DESIGNED TO ALERT THE USER TO ANY
    PROBLEMS WITH THE PRESSURE.

    APPENDS the pressure to a file!!!

    ios::app == open file for appending
    ios::trunc == delete information in file // say IF entries>12

    I would like to add a default (press return) process!!! (how!)
    i.e cout<<"pressure reading <100> ";//if user enters return pressure is set to 100

    ************************************************** ******************/

    #include<iostream.h>
    #include<fstream.h>
    #include<iomanip.h>

    float voltage;//assign memory space
    float pressure;
    float riser1;//used to record whether the temp rises consecutively
    float riser2;//
    int count;//used to count entries before "ios::trunc" (clearing file)
    int rcount;//riser counter



    void main()
    {
    char rnumber[12][2];
    char reading[12][5];


    ofstream outfile;//setting up stream (called outfile)

    outfile.open("C:\\pressure.txt",ios::trunc);//clear existing file before the loop starts
    outfile.close();//close it

    rcount=0;
    riser1=0;//re-setting
    riser2=0;//


    for(count=0;count<12;count++)//setting counter for loop (12 months)
    {//begin for loop1


    cout<<"Enter the voltage reading: <"<<voltage<<">";//get input
    cin>>voltage;

    //if(voltage== )//if voltage == user return
    //{
    // voltage=voltage;//set voltage to default
    //}


    //convert voltage to pressure

    pressure=((voltage*20)+500);




    if(pressure>=700)
    {
    cout<<"\n\..........************* WARNING THE PRESSURE IS ABOVE RECOMENDED LEVELS!****************"<<endl;
    }

    riser2=pressure;

    if(riser2>riser1)
    {
    rcount++;
    }//end if
    else
    {
    rcount=1;
    }

    if(rcount>=4)
    {

    cout<<"\n\..........*********** WARNING! THE LAST "<<rcount<<" ENTRIES HAVE RISEN CONSECUTIVELY *************\n";
    //rcount=0;//reset the counter
    }//end if


    /******************** store reading ************************/


    //target stream (outfile) to a text file (pressure.txt)
    //then open it ready to append data

    outfile.open("C:\\pressure.txt",ios::app);

    //send data

    if(pressure>=700)//if pressure is >= 700 print asterix's as warning in file
    {
    outfile<<"***"<<pressure<<"***"<<endl;
    }//end if

    else//otherwise just print the pressure
    {
    outfile<<pressure<<endl;
    }//end if

    cout<<endl<<"file written\n";

    outfile.close();//do I need this with a write file???



    /********************************/







    riser1=riser2;//set riser1 equal to riser2 (helps keep track of the consecutive readings)



    cout<<"temp print out of pressure "<<pressure<<endl<<endl;
    }//end for loop1
    }//end main

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    > is it possible to accept the user pressing return when prompted
    to enter a value???
    Only if you accept user input as a string.

    Here's how it might look
    Code:
    #include <stdlib.h>
    #include <string.h>
    
    int main ( ) {
        char    buff[100];
        float   f;
        cin.getline( buff, 100 );
        if ( strlen(buff) == 0 ) {
            f = 100;
        } else {
            f = atof( buff );
        }
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    newby
    Guest
    of course!
    thanks for that
    mik

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Utilizing another compiled program for a task.
    By kotoroshinoto in forum C Programming
    Replies: 6
    Last Post: 06-03-2008, 02:43 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 05:24 PM
  4. Help with default parameter value using ptr-to-functor
    By registering in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2004, 04:21 PM
  5. Switching Default Buttons :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 07-02-2002, 05:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21