Menu

cin chars & arrays

2002-09-24
2012-09-26
  • Nobody/Anonymous

    whats wrong here?! how do i cin a multiple word (including spaces) char?

    #include <iostream.h>

    int main()
    {
    char name[250];

    cout<<"Name: ";
    cin>>name;

    cout<<"Name: "<<name<<endl;
    system("pause");
    return 0;
    }

     
    • Nobody/Anonymous

      #include <iostream>
      #include <string.h>
      #include <ctype.h>
      #define MAX 250

      int main()
      {
         char name[MAX];
         int x;

         std::cout << "Name: ";
         std::cin >> name;
         for (x=0; x<strlen(name); x++)
           {
              name[x] = toupper(name[x]);
            }
         std::cout << std::endl << name << std::endl;
         system ("PAUSE");
         return (0);
      }

      This is how I'd do it, but I'm sure there may be a better way. I'm relatively new to this.
      Amy

       
    • Nobody/Anonymous

      #include <iostream>
      #include <string.h>
      #include <ctype.h>
      #include <stdlib.h>  // need for system ("PAUSE"); below.
      #define MAX 250

      using namespace std;  // add this and don't need 'std::'

      int main()
      {
      char name[MAX];
      int x;

      cout << "Name: ";
      cin >> name;
      for (x=0; x<strlen(name); x++)
      {
      name[x] = toupper(name[x]);
      }
      cout << endl << name << endl;
      system ("PAUSE");
      return (0);
      }

         j@ck_

       
    • Nobody/Anonymous

      // Cut-nPaste this into dev. No project needed.

      #include <iostream>
      #include <iomanip>
      #include <windows.h>
      #include <stdio.h> // for the getchar() below
      #include <conio.h>

      using namespace std;

      void SetColor(short Color)
      {
      HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
      SetConsoleTextAttribute(hCon,Color);
      }

      int main()
      {
      SetColor(75); // 7=bkgcolor & 5=textcolor.
         
      cout.setf(ios::fixed);                     

      double salePrice = 0;
      double discnt = 0;
      double oPFactor = 1;
          
      // Simple little program to find the original price.
      // ! ! Press enter after each cin..   press enter to close at program's end. ! !    
      cout << "    FIND THAT PRICE " << endl;
      cout << " Enter the sale price you paid, and the Discount(%).[as example: .10 = 10%]" << endl << endl;
      cout << endl << endl;
          
      cout << endl;
          
      cout << " Enter the salePrice " << endl << endl;
      cout << ": ";
      cin >> salePrice;                
      cout << endl;
                                         
      cout << " Enter the Discount " << endl << endl;
      cout << ": " ;
      cin >> discnt;            
      cout << endl;
              
      double  oPrice = (salePrice /(oPFactor-discnt));
             
      SetColor(94);    
      cout << endl
      << "The Price befoe the Discount was:" 
      << setprecision(2) << setw(14)<< oPrice <<"  Press Enter"<< endl;
             
      getchar();
      return 0;
      }  
          

        j@ck_

       
    • Nobody/Anonymous

      //Start of Program
      #include <iostream.h>
      #include <stdlib.h>

      int main()
      {
          char name[50];
          cout << "Enter full name\n";
          cin.getline(name,50);
          cout << "Your name is " << name << endl;
          system("PAUSE");
      }
      //End of program

      cin.getline(char* , MAX_LENGTH, ACCEPT_UP_TO = '\n')

      If you run the program, and use "Curtis Sutter" as your input, try changing the cin.getline(name,50); to cin.getline(name,50,'i');
      Normally the getline function defaults to the '\n' character but changing this will make accept up to that character.

      cin.getline(name,50); // Curtis Sutter as input
      cout << name;  //outputs Curtis Sutter

      cin.getline(name,50,'t'); // Curtis Sutter as input
      cout << name;  //outputs Cur

      Be forwarned that if you do not include a
      cin.ignore(100,'\n');
      when using something like cin.getline(name,50,'t'); anything not accepted is left in the buffer.

      Try this:

      //Start of Program
      #include <iostream.h>
      #include <stdlib.h>

      int main()
      {
          char name[50];
          cout << "Enter full name\n";
          cin.getline(name,50,'t');
          cout << "Your name is " << name << endl;
          cin.getline(name,50);
          cout << "Your name is " << name << endl;
          system("PAUSE");
      }
      //End of Program

      Hope all of this has been of a benifit to you.  Feel free to write me at curtis8@hotmail.com if you have any more questions of this type.

      Curtis

       
    • Nobody/Anonymous

      #include <cstdlib>
      #include <cstdio>
      char name [MAX_PATH];
      int main ()
      {
      printf ("Name: ");
      gets (name);
      printf ("Name: %s\n", name);
      system ("pause");

      Outputs exactly as many words as you input. Cin uses scanf for input and scanf reads only first word from the string.

      tkorrovi

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.