How to read the data as a whole string?

This is a discussion on How to read the data as a whole string? within the C++ Programming forums, part of the General Programming Boards category; Why are you accepting 2 array of strings into a function like that? Why are you uppercasing 25 characters for ...

  1. #16
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Why are you accepting 2 array of strings into a function like that? Why are you uppercasing 25 characters for each string? Or are you trying to call toupper() on 25 strings? toupper() is meant to work on only one letter at a time, not entire strings.

    I think you're rushing through this way too quickly. If you waited until the last minute to do this assignment, then you're in trouble, and we probably can't help you. If you have a week or so before it's due, or heck, possibly even a few days left to hand it in, then you have time to slow down, think things through, and get it done right.

    What are you really trying to do? Just convert a string to uppercase? Do you need to keep the original string? From what I can see you don't need the original lastname in the program, so why not just convert it?

    Code:
    void ChangeLastname(string &lastname)
    {
    	for(size_t i=0;i<lastname.length();i++)
    	{
    		lastname[i] = toupper(lastname[i]);
    	}
    }
    Now if you have a string named lastname in main() or somewhere else, you can call the above function like this:

    Code:
    ChangeLastname(lastname);
    Bingo. lastname will now be completely capitalized.

    When you read in lines, you should read in one line at a time in a loop and deal with it. Inside this loop, that's where you need to call ChangeLastname().
    Last edited by MacGyver; 05-28-2007 at 05:42 PM.

  2. #17
    Registered User
    Join Date
    May 2007
    Posts
    67
    Quote Originally Posted by MacGyver View Post
    Why are you accepting 2 array of strings into a function like that? Why are you uppercasing 25 characters for each string? Or are you trying to call toupper() on 25 strings? toupper() is meant to work on only one letter at a time, not entire strings.

    I think you're rushing through this way too quickly. If you waited until the last minute to do this assignment, then you're in trouble, and we probably can't help you. If you have a week or so before it's due, or heck, possibly even a few days left to hand it in, then you have time to slow down, think things through, and get it done right.

    What are you really trying to do? Just convert a string to uppercase? Do you need to keep the original string? From what I can see you don't need the original lastname in the program, so why not just convert it?

    Code:
    void ChangeLastname(string &lastname)
    {
    	for(size_t i=0;i<lastname.length();i++)
    	{
    		lastname[i] = toupper(lastname[i]);
    	}
    }
    Now if you have a string named lastname in main() or somewhere else, you can call the above function like this:

    Code:
    ChangeLastname(lastname);
    Bingo. lastname will now be completely capitalized.

    When you read in lines, you should read in one line at a time in a loop and deal with it. Inside this loop, that's where you need to call ChangeLastname().
    I have tried a lot,but failed.I am a super beginner,and what you did I never learned in my class and my book.I just know toupper is used to convert,but not that much.
    Maybe I am a fool in programming.But I will not give up,at least now.Maybe I should see some good books which are for beginners.

  3. #18
    Registered User
    Join Date
    May 2007
    Posts
    67
    I already finish my code,but I have some strange errors.Please help to fix them.I put my code in #1 floor.

  4. #19
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    When you're brand new, it's not necessarily supposed to be easy. If it was really easy, everyone would be programming away, making programs they want instead of paying money for them.

    What you need to do is go through the basics again. Perhaps your professor didn't cover them right, or you slacked off and didn't pay attention, or perhaps both. Super beginner or not, you have to learn this by doing it, but before you can do it, you have to actually learn it.

    Looking through your main() function, it seems you need to go through your text book again (or whatever resources you have that teach the basics). Function prototypes are not function calls.

    1.)"implicit declaration of function `swapValues2(...)'"and also for swapValues.
    Put function prototypes near the top of your .cpp file, right after your includes.

    (2.)"temp,v1,v2"undeclared and also"
    Possibly something to do with the first error if this is because of the swapValues2() function being considered messed up.

    (3.) warning: cannot pass objects of type `basic_string<char,string_char_traits<char>,__defa ult_alloc_template<false,0> >' through `...'".
    Don't know what line that is for, but it's because you're not handling a string properly, and it's being treated as if it's a different data type.
    Last edited by MacGyver; 05-28-2007 at 08:15 PM.

  5. #20
    Registered User
    Join Date
    May 2007
    Posts
    67
    Quote Originally Posted by MacGyver View Post
    When you're brand new, it's not necessarily supposed to be easy. If it was really easy, everyone would be programming away, making programs they want instead of paying money for them.

    What you need to do is go through the basics again. Perhaps your professor didn't cover them right, or you slacked off and didn't pay attention, or perhaps both. Super beginner or not, you have to learn this by doing it, but before you can do it, you have to actually learn it.

    Looking through your main() function, it seems you need to go through your text book again (or whatever resources you have that teach the basics). Function prototypes are not function calls.



    Put function prototypes near the top of your .cpp file, right after your includes.



    Possibly something to do with the first error if this is because of the swapValues2() function being considered messed up.



    Don't know what line that is for, but it's because you're not handling a string properly, and it's being treated as if it's a different data type.

    Thank you for your patience and your good advice,I will try it.Hope that one day I can do as well as you do.

  6. #21
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by tx1988 View Post
    Thank you for your patience and your good advice,I will try it.Hope that one day I can do as well as you do.
    You can get to a much higher level in C++ than I'm at. I might look like I know what I'm doing at times, but in reality I'm not nearly as good as many of the real experts on here.

    I do know enough, though, to say that what it mainly takes to get good at coding is motivation, the ability to learn and find solutions to problems you have not encountered, and experience.

    On the subject of experience, if you learn a concept in class, actually test it out on the computer. Write a simple program just to test it out, even if it's only a few lines to make sure you know how to do what you've been taught. I've been programming for years, and I still write test applications to make sure I know what I'm doing when I feel it necessary before I write the actual program that I wanted to make in the first place. Better to find out you were wrong in a 10 line program than a 1,000 line program.

  7. #22
    Registered User
    Join Date
    May 2007
    Posts
    67
    Quote Originally Posted by MacGyver View Post
    You can get to a much higher level in C++ than I'm at. I might look like I know what I'm doing at times, but in reality I'm not nearly as good as many of the real experts on here.

    I do know enough, though, to say that what it mainly takes to get good at coding is motivation, the ability to learn and find solutions to problems you have not encountered, and experience.

    On the subject of experience, if you learn a concept in class, actually test it out on the computer. Write a simple program just to test it out, even if it's only a few lines to make sure you know how to do what you've been taught. I've been programming for years, and I still write test applications to make sure I know what I'm doing when I feel it necessary before I write the actual program that I wanted to make in the first place. Better to find out you were wrong in a 10 line program than a 1,000 line program.
    Great experience!Thank you for sharing!Maybe you are not the best,but you will be my first destination!hahaha!

  8. #23
    Registered User
    Join Date
    May 2007
    Posts
    67
    Code:
    void ChangeLastname(string &lastname)
    {
    	for(size_t i=0;i<lastname.length();i++)
    	{
    		lastname[i] = toupper(lastname[i]);
    	}
    }
    I try your code in my program,but it has some error in your code,since you can see from my code,the complier said "declaration of `lastname' as array of references",so I think my function doesn't take array.I don't know why because I haven't learn much enough, such as .length.

  9. #24
    Registered User
    Join Date
    May 2007
    Posts
    67
    I found I can convert string to C-string and then use toupper on each char.But I want to know if I do it in your way,how to fix?

  10. #25
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Depends on the purpose of what you want the function to do. I wrote it to convert one string to uppercase. In that sense, my code is correct. If you want it to accept an array, that's different.

    You'd have to pass a pointer representing the array, and..... uh... yeah.

    I think you're overcomplicating this. Can you write a function to read just one one of the lines from the file, alter it like you're supposed to, and then output it to a file?

    So from the assignment, you would first read this:

    Mary Jones: 89 90 100
    You would then open your output file and write this:

    JONES Mary: 89 90 100
    Once you can do that, you can do the rest of the assignment with the same code by just altering it a little.

  11. #26
    Registered User
    Join Date
    May 2007
    Posts
    67
    Hi,guys!In the last,with hours of hard work,I finish it without any errors,but for the output it is really bad.So last thing,help me fix the output!


    Code:
     #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    #include <string>
    #include <iomanip>
    using namespace std;
    void loadData(string firstname[],string lastname[],int score1[],int score2[],int score3[]) 
    {
         int i=0;
         ifstream in;
         in.open("F:\\test 4.d\\New Folder (4)\\Data.dat");
         if(in.fail())
         {
            in.close();
            cout<<"Data.txt not found,program ending!"<<endl;
            system("Pause");
            exit(1);
         }
    
    
           for(int i=0;i<25;i++)
         {
               in >> firstname[i] >> lastname[i] >> score1[i] >> score2[i] >> score3[i];
         }
    }
    void ChangeLastName(string lastname[25])
    {
        for(int i=0; i<25; i++)
        {
            string tmp = lastname[i];
            char temp[tmp.length()];
            strcpy(temp, tmp.c_str());
            for(int j=0; j<tmp.length(); j++)
            {
                temp[j] = toupper(temp[j]);
            }
            lastname[i] = temp;
        } 
    }
    
    void swapValues(string& v1,string& v2)
    {
             string temp;
              temp=v1;
              v1=v2;
              v2=temp; 
    }
    void swapValues(int& v1,int& v2)
    {
         int temp;
         temp=v1;
         v1=v2;
         v2=temp;
    }
    void sorting(string lastname[],string firstname[],int score1[],int score2[],int score3[])
    {
        for(int i=0;i<25;i++)
        {
            for(int j=0; j<25; j++)
            {
                if(lastname[i] < lastname[j])
                  {  swapValues(lastname[i],lastname[j]);
                     swapValues(firstname[i],firstname[j]);
                     swapValues(score1[i],score1[j]);
                     swapValues(score2[i],score2[j]);
                     swapValues(score3[i],score3[j]);  }
    
           }
    }
    }
    void grade(int score1[],int score2[],int score3[],double ave[],double aveScore[],double sum[],string rank[])
    {
        sum[0]=0,sum[1]=0,sum[2]=0;
        for(int i=0;i<25;i++)
        {
            ave[i]=(score1[i]+score2[i]+score3[i])/3.;
            if(ave[i]>=90)
               rank[i]="A";
            else if(ave[i]<90&&ave[i]>=80) 
               rank[i]="B";
            else if(ave[i]<80&&ave[i]>=70)
               rank[i]="C";
            else if(ave[i]<70&&ave[i]>=60)
               rank[i]="D";
    
            sum[0]+=score1[i];
            sum[1]+=score2[i];
            sum[2]+=score3[i];
         }
    
          aveScore[0]=sum[0]/25.;
          aveScore[1]=sum[1]/25.;
          aveScore[2]=sum[2]/25.;
    }
    void WriteToFile(string lastname[],string firstname[],int score1[],int score2[],int score3[],double ave[],double aveScore[],string rank[])
    {
         int i=0,j=0;
         ofstream out;
         out.open("F:\\test 4.d\\New Folder (4)\\Data2.dat");
         if(out.fail())
         {
            out.close();
            cout<<"Data2.txt not found,program ending!"<<endl;
            exit(1);
         }
         out.setf(ios::fixed);
         out.setf(ios::showpoint);
         out.precision(2);
         out<<"LASTNAME"<<setw(20)<<"FIRSTNAME"<<setw(10)<<"SCORE1"<<setw(10)<<"SCORE2"<<setw(10)<<"SCORE3"<<setw(10)<<"RANK"<<endl;
         out<<"*************************************************************************************************************************************"<<endl;
         for(int i=0;i<25;i++)
       { out<<lastname[i]<<","<<setw(20)<<firstname[i]<<":"<<score1[i]<<setw(10)<<score2[i]<<score3[i]<<ave[i]<<rank[i]<<endl;
               }
         out<<"*************************************************************************************************************************************"<<endl;
         out<<"TEST AVERAGE"<<setw(30)<<aveScore[0]<<setw(10)<<aveScore[1]<<setw(10)<<aveScore[2]<<endl;
       }
     
    int main(int argc, char *argv[])
    {
       string firstname[25], lastname[25],rank[25];
       int score1[25], score2[25], score3[25],score[3];
       double aveScore[3],sum[3],ave[25];
       loadData(firstname,lastname,score1,score2,score3);
       ChangeLastName(lastname);
       ChangeLastName(firstname);
       sorting(lastname,firstname,score1,score2,score3);
       grade(score1,score2,score3,ave,aveScore,sum,rank);
       WriteToFile(lastname,firstname,score1,score2,score3,ave,aveScore,rank);
       system("pause");
       return 0;
    }
    this is my output!
    Attached Images Attached Images  

  12. #27
    Registered User
    Join Date
    May 2007
    Posts
    67
    I already know how to fix,just add setw(10) in out.,before lastname.Then it became pretty.
    This is the first project make me feel hard,but I really learn something in it.I mean I enjoy solving it.Thank you for everyone's help!!!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM

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