Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Spaces in strings - how?
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Srlancelot39




PostPosted: Wed Feb 09, 2011 12:49 am   Post subject: Spaces in strings - how?

I am making a program that may result in the user entering a string containing spaces, and I need the program to accept the entire input (including spaces and characters after a space) and store it in the variable. Currently the program is carrying over words after a space into the next get.
Example (pseudocode and output):

get var1

(input: word1 word2)

get var2 - this ends up taking word2 as its value

print var1,var2

output: word1,word2


I know this example looks like crap, but basically, the program is carrying over the input. How do I get it so that var1 is holding "word1 word2"?

Thank you!
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed Feb 09, 2011 1:03 am   Post subject: RE:Spaces in strings - how?

I think you are missing some arguments there... http://www.cplusplus.com/reference/iostream/istream/get/
code:

char str[256];
cin.get (str,256);

Should work with spaces.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Srlancelot39




PostPosted: Wed Feb 09, 2011 1:19 am   Post subject: RE:Spaces in strings - how?

do i have to use 'str' or is that where my variable name goes?
Srlancelot39




PostPosted: Wed Feb 09, 2011 1:23 am   Post subject: Re: Spaces in strings - how?

// Assignment1 2.13.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <string.h>
#include "iostream"
#include "conio.h"
#include <fstream>

using namespace std;
int main()
{
string driveletter;
char pathname[256];
string filename;
string extension;
cout << "Welcome to FileMaster!" << endl;
cout << "Please enter the drive letter (i.e. 'C'): ";
cin >> driveletter;
cout << "Please enter the path name (i.e. \\Windows\\System): ";
cin.get (pathname,256);
cout << "Please enter the file name (i.e. Readme): ";
cin >> filename;
cout << "Please enter the file extension (i.e. txt): ";
cin >> extension;
cout << driveletter << ":" << pathname << "\\" << filename << "." << extension << endl;
getch();
return 0;
}


now its ignoring the other cin statements after i enter a value for the first one....lol
Xupicor




PostPosted: Tue Feb 22, 2011 5:58 pm   Post subject: RE:Spaces in strings - how?

Why not make use of std::string:
c++:
#include <iostream>
#include <string>

int main() {
    using namespace std;
    string s;
    getline(cin, s); // reads up to (including) \n, and discards \n char.
    cout << s << '\n';
    return 0;
}
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: