Thread: printf problem

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    72

    printf problem

    I would like to know how to replace the text inside the "printf"statement with a variable which is a double.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
       /* redirect standard output to a file */
       if (freopen("OUTPUT.txt", "w", stdout)
           == NULL)
          fprintf(stderr, "error redirecting stdout\n");
    
       /* this output will go to a file */
       printf("This will go into a file.");
    
       /* close the standard output stream */
       fclose(stdout);
    
       return 0;
    }
    its ungent thanks

  2. #2
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    Hmm, ungent? Do you mean urgent? Uh-oh...

    Hmm, printf()=C, not C++, right?

    Hmm, how about
    Code:
    printf("%d", myDouble);
    I don't know much printf, so d might me the wrong watchyamacallit?

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    printf("%f", myDouble);

    EDIT - and C is a subset of C++ so it's all good
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    72
    how would you put a real number in c++ in a variable inside the printf statement.

  5. #5
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    C++ uses cout...

    Code:
    cout<<myDouble;

  6. #6
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    I think printf is deprecated in C++, I didnt touch it Cos I start with C++, lol~
    Never end on learning~

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    72
    also how do you add a blank space using printf(); and what would be the statement for if it was an integer instead.
    Last edited by niroopan; 10-07-2002 at 07:35 PM.

  8. #8
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by niroopan
    also how do you add a blank space using printf();
    you must be missing the basic concept of printf here. printf takes your format string and inserts the variables you give it in to it where the various % codes are. so to print a double

    printf ("%f", mydouble);

    to print a double with a space after it

    printf ("%f ", mydouble);

    also, you really shouldnt be using freopen here, theres no need to. just use

    fprintf (myfile, "%f ", mydouble);

    (that is after you fopen() myfile of course)
    hello, internet!

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    double foo=3.14127;
    printf("the value of foo is %f",foo);
    printf("  ");
    printf("which also happens to be the value of PI\n");
    // or to put it all together
    
    printf("the value of foo is %f  which also happens to be the value of PI\n",foo);
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    72
    the real code is this,

    Code:
    //Author: Gavinn Niroopan//
    //Program Name: ngsummary.h//
    //Revision Date: 2002-10-11//
    //Explanation: to use variables and loopibf structures display a summary,the following assignment will demonstrate a program structure, code layout and use logic structure//
    
    #include <iostream.h>	//allows cout statement//
    #include <iomanip.h>   	//allows decimal formatting, setiosflags()//
    #include <conio.h>     	//allows clrscr() and getch()//
    #include <string.h>    	//allows strcpy()//
    #include <time.h>      	//allows the call of time function//
    #include <stdio.h>      //allows printf statement//
    
    int main(void)
    {
    	int studcountmax, year;
       double term1, term2, studaverage, classtotal;
    
       time_t t=time(NULL); //time function//
    								//Fowler Statement//
       setiosflags(ios::right|ios::fixed);
       							//Will display the time//
       cout <<setw(79)<<ctime(&t)<<endl;
       cout<<setw(79)<<"Gavinn Niroopan"<<endl;
       cout<<setw(79)<<"2002-10-11"<<endl;
       cout<<setw(79)<<"Mr. Slack"<<endl;
       cout<<setw(79)<<"ICS 3MO-A"<<endl;
       gotoxy(32,8); 			//moves cursor to location (x,y) on screen//
       cout<<"Summary Program"<<endl;
       gotoxy(32,9);
       cout<<"~~~~~~~~~~~~~~~"<<endl;
       							//"for" loop for spacing//
       	for(int i= 1;i <=6;i++)
          	cout<<endl;
       cout<<"Explanation:"<<endl;
    	cout<<"~~~~~~~~~~~"<<endl<<endl;
       cout<<"\tTo create a program which will use variables and looping structures"<<endl;
       cout<<"\tto display a summary sheet. The following assignment will demonstrate"<<endl;
       cout<<"\ta program structure, code layout and use logic structures. The program"<<endl;
       cout<<"\tThe program will use data derived from the user and date acquired from"<<endl;
       cout<<"\the system at time of execution."<<endl;
    
       getch();					//Pause screen//
       clrscr();				//clear screen//
    
       cout<<ctime(&t)<<endl<<endl;
       gotoxy(29,4);
       cout<<"Class Summary Analysis"<<endl;
       gotoxy(29,5);
       cout<<"~~~~~~~~~~~~~~~~~~~~~~"<<endl<<endl;
       cout<<"\tHow many students are being processed:     ";
       cin>>studcountmax;
       cout<<"\tWhat school year is being processed:       ";
       cin>>year;
    
       clrscr();				//clear screen//
    
       for(int studcount=1;studcount<=studcountmax;studcount++)
       	{
       		cout<<ctime(&t)<<endl<<endl;
       		gotoxy(26,4);
       		cout<<"Student Term Evaluation Record"<<endl;
       		gotoxy(26,5);
       		cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl<<endl;
       		cout<<"\t\tEnter the 1st term mark: - ";
       		cin>>term1;
             cout<<"\t\tEnter the 2nd term mark: - ";
             cin>>term2;
    
             studaverage = (term1 + term2)/2;
             classtotal = classtotal + studaverage;
    
       	   // redirect standard output to a file //
       		if (freopen("OPEN.txt", "a", stdout)== NULL)
    
          		fprintf(stderr, "error redirecting stdout\n");
    
       		//this output will go to a file //
             {
             printf("Student ",studcount,"%f",studaverage);
             cout<<endl;
             }
    
             // close the standard output stream //
             fclose(stdout);
             clrscr();
         	}
    
    
    
       getch();
    
       return 0;
    }
    I want it to look like this in the txt file;

    student 1 63.00%
    student 2 78.00%

    etc. but its not working properly.

  11. #11
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    stop using freopen.
    and do something like this (code may not be 100% accurate)

    fout << "Student " << studcount << studaverage << '%' << endl;

    or with fprintf

    fprintf (myfile "Student %i %.2f%%\n", studcount, studaverage);
    hello, internet!

  12. #12
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    why redirect stdout when you could use fopen or fstream classes which would make this a piece of cake.
    On the whole its probably not a good idea to mix c and c++ i/o routines. stick to one or the other.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  13. #13
    Registered User
    Join Date
    Sep 2002
    Posts
    72
    Code:
    fprintf (myfile "Student %i %.2f%%\n", studcount, studaverage);
    how and where would you write the line in my code and how would i write to filename as open.txt instead of myfile.

  14. #14
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    you mean to say you know how to use freopen() but not fopen()????
    hello, internet!

  15. #15
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by niroopan
    the real code is this,

    Code:
    //Author: Gavinn Niroopan//
    //Program Name: ngsummary.h//
    //Revision Date: 2002-10-11//
    //Explanation: to use variables and loopibf structures display a summary,the following assignment will demonstrate a program structure, code layout and use logic structure//
    
    #include <iostream.h>	//allows cout statement//
    #include <iomanip.h>   	//allows decimal formatting, setiosflags()//
    #include <conio.h>     	//allows clrscr() and getch()//
    #include <string.h>    	//allows strcpy()//
    #include <time.h>      	//allows the call of time function//
    #include <stdio.h>      //allows printf statement//
    
    int main(void)
    {
    	int studcountmax, year;
       double term1, term2, studaverage, classtotal;
    
       time_t t=time(NULL); //time function//
    								//Fowler Statement//
       setiosflags(ios::right|ios::fixed);
       							//Will display the time//
       cout <<setw(79)<<ctime(&t)<<endl;
       cout<<setw(79)<<"Gavinn Niroopan"<<endl;
       cout<<setw(79)<<"2002-10-11"<<endl;
       cout<<setw(79)<<"Mr. Slack"<<endl;
       cout<<setw(79)<<"ICS 3MO-A"<<endl;
       gotoxy(32,8); 			//moves cursor to location (x,y) on screen//
       cout<<"Summary Program"<<endl;
       gotoxy(32,9);
       cout<<"~~~~~~~~~~~~~~~"<<endl;
       							//"for" loop for spacing//
       	for(int i= 1;i <=6;i++)
          	cout<<endl;
       cout<<"Explanation:"<<endl;
    	cout<<"~~~~~~~~~~~"<<endl<<endl;
       cout<<"\tTo create a program which will use variables and looping structures"<<endl;
       cout<<"\tto display a summary sheet. The following assignment will demonstrate"<<endl;
       cout<<"\ta program structure, code layout and use logic structures. The program"<<endl;
       cout<<"\tThe program will use data derived from the user and date acquired from"<<endl;
       cout<<"\the system at time of execution."<<endl;
    
       getch();					//Pause screen//
       clrscr();				//clear screen//
    
       cout<<ctime(&t)<<endl<<endl;
       gotoxy(29,4);
       cout<<"Class Summary Analysis"<<endl;
       gotoxy(29,5);
       cout<<"~~~~~~~~~~~~~~~~~~~~~~"<<endl<<endl;
       cout<<"\tHow many students are being processed:     ";
       cin>>studcountmax;
       cout<<"\tWhat school year is being processed:       ";
       cin>>year;
    
       clrscr();				//clear screen//
    
       for(int studcount=1;studcount<=studcountmax;studcount++)
       	{
       		cout<<ctime(&t)<<endl<<endl;
       		gotoxy(26,4);
       		cout<<"Student Term Evaluation Record"<<endl;
       		gotoxy(26,5);
       		cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl<<endl;
       		cout<<"\t\tEnter the 1st term mark: - ";
       		cin>>term1;
             cout<<"\t\tEnter the 2nd term mark: - ";
             cin>>term2;
    
             studaverage = (term1 + term2)/2;
             classtotal = classtotal + studaverage;
    
       	   // redirect standard output to a file //
       		if (freopen("OPEN.txt", "a", stdout)== NULL)
    
          		fprintf(stderr, "error redirecting stdout\n");
    
       		//this output will go to a file //
             {
             printf("Student ",studcount,"%f",studaverage);
             cout<<endl;
             }
    
             // close the standard output stream //
             fclose(stdout);
             clrscr();
         	}
    
    
    
       getch();
    
       return 0;
    }
    I want it to look like this in the txt file;

    student 1 63.00%
    student 2 78.00%

    etc. but its not working properly.
    what are those braces supposed to be enclosing? there's no loop or conditional starting there
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems reading entered race times C
    By loopymoo26 in forum C Programming
    Replies: 12
    Last Post: 05-23-2009, 07:38 AM
  2. Help with a C Programing Quiz project
    By dilemma in forum C Programming
    Replies: 12
    Last Post: 05-15-2009, 03:35 PM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. error in program????
    By SpEkTrE in forum C Programming
    Replies: 5
    Last Post: 11-24-2003, 06:16 PM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM